Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Good way to edit the previous defined class in ipython

Tags:

python

ipython

I am wondering a good way to follow if i would like to redefine the members of a previous defined class in ipython. say : I have defined a class intro like below, and later i want to redefine part of the function definition _print_api. Any way to do that without retyping it .

class intro(object):
   def _print_api(self,obj):
           def _print(key):
                   if key.startswith('_'):
                           return ''
                   value = getattr(obj,key)
                   if not hasattr(value,im_func):
                           doc = type(valuee).__name__
                   else:
                           if value.__doc__ is None:
                                   doc = 'no docstring'
                           else:
                                   doc = value.__doc__
                   return '        %s      :%s' %(key,doc)
                   res = [_print(element) for element in dir(obj)]
                   return '\n'.join([element for element in res if element != ''])
   def __get__(self,instance,klass):
           if instance is not None:
                   return self._print(instance)
           else:
                   return self._print_api(klass)
like image 922
leo Avatar asked Apr 06 '10 15:04

leo


People also ask

How do you clean IPython shells?

To clear the screen on Windows, use ! CLS . On Unix-like systems, use ! clear .

Which of the following is used to know about an object in IPython?

The command whos and linemagic %whos are available in IPython, but are not part of standard Python. Both of these will list current variables, along with some information about them. You can specify a type to filter by, e.g.

How do you check methods in Jupyter notebook?

Jupyter Notebook can show that documentation of the function you are calling. Press Shift+Tab to view the documentation. This is very helpful as you don't need to open the documentation website every single time. This feature also works for the local custom functions.


2 Answers

Use the %edit command, or its alias %ed. Assuming that the intro class already exists in the ipython namespace, typing %ed intro will open an external editor on the source code for the class. When you save and exit the editor the code will be executed by ipython, effectively redefining the class.

The downside of this is that any instances that already exist will still be bound to the old version of the class - if this is an issue then you need to either recreate the objects or re-assign the obj.class attribute to point to the new version of the class.

You can also use %ed on modules, files and previous input lines, e.g. %ed 5 10:13 16 will create and edit a file composed of ipython input lines 5,10,11,12,13,16.

like image 155
Dave Kirby Avatar answered Oct 12 '22 22:10

Dave Kirby


If you use the IPython %edit features, you can use something like this

like image 36
unode Avatar answered Oct 12 '22 23:10

unode