[IPython-user] Printing the doc of the class object instead of the object instance

David Huard david.huard at gmail.com
Wed Jun 21 13:22:31 CDT 2006

Hi all, 

Maric Michaud suggested a solution on comp.python.general. 

By putting his script in the .ipython directory and running it in the
pythonrc file with execfile, I get the desired behaviour. Pretty neat.


#replace the standard magic_pinfo of ipython
#This one will insert __class__. before the last element if it is a property."""
# Author: Maric Michaud
# June 2006

if not hasattr(__IPYTHON__, 'old_pinfo') :
    __IPYTHON__._old_pinfo = __IPYTHON__.magic_pinfo

    def new_pinfo(obj) :
        """Redefines pinfo in order to print the doc string of the class 
	attribute instead of the instance. 
        obj param is the string typed in the interpreter, ie a.b.c it will
	 be evaluated in magic_pinfo"""
	path = obj.split('.')
        if len(path) == 1 :
            return __IPYTHON__._old_pinfo(obj)
        else :
            new_eval_string = '.'.join(path[:-1])
            # get the last attribute by the mean of the __class__ of its owner
            target = __IPYTHON__.user_ns[path[0]] # user_ns is the globals of the interpreter
            for attr in path[1:-1] :
                target=getattr(target, attr)
            target = getattr(target.__class__, path[-1], None)

            if isinstance(target, property) :
                new_eval_string += '.__class__'
            return __IPYTHON__._old_pinfo(new_eval_string + '.' + path[-1])

    __IPYTHON__.magic_pinfo = new_pinfo

More information about the IPython-user mailing list