Question

I like that IPython will fetch docstrings if I type foo.bar?

However, I may sometimes build the foo.bar method dynamically, using foo.__getattr__. I could conceivably also generate a docstring dynamically, perhaps in a magic method like foo.__getdoc__.

Does IPython provide any mechanism for doing this, such that it would discover and display docstrings built on the fly?

Was it helpful?

Solution 2

That has nothing to do with IPython, IPython just reads the __doc__ attribute of an object. If you dynamically create your objects in the __getattr__ method you should just set their __doc__ attribute as well and everything will work correctly in IPython.

Here is an example:

class A(object):
    def __init__(self, name):
        self.name = name

    def __repr__(self):
        return 'A[%s]' % self.name

class Test(object):
    def __getattr__(self, name):
        a = A(name)
        a.__doc__ = 'My a documentation'
        return a

In [11]: t = Test()
In [12]: t.foo
A[foo]
In [13]: t.foo?
Type:       A
String Form:A[foo]
Docstring:  My a documentation

OTHER TIPS

You can set foo.__doc__ = "my doc string".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top