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)
To clear the screen on Windows, use ! CLS . On Unix-like systems, use ! clear .
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.
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.
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.
If you use the IPython %edit features, you can use something like this
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With