Link to class method in python docstring Link to class method in python docstring python python

Link to class method in python docstring


The solution that works for Sphinx is to prefix the reference with ~.

Per the Sphinx documentation on Cross-referencing Syntax,

If you prefix the content with ~, the link text will only be the last component of the target. For example, :py:meth:~Queue.Queue.get will refer to Queue.Queue.get but only display get as the link text.

So the answer is:

class MyClass():    def foo(self):        print 'foo'    def bar(self):        """This method does the same as :func:`~mymodule.MyClass.foo`"""        print 'foo'

This results in an html looking like this : This method does the same as foo(), and foo() is a link.

However, note that this may not display in Spyder as a link.


If you want to manually specify the text of the link you can use:

:func:`my text <mymodule.MyClass.foo>`

For more information, checkout Cross-referencing Python objects.


It seems to me that you just have to add __name__ or __doc__ to your expression to obtain what you want.
I'm still not sure to have correctly understood the aim

class MyClass():    def foo(self):        """I am the docstring of foo"""        print 'foo'    def bar(self):        """This method does the same as <link to foo>"""        print 'foo'printprint MyClass.fooprint MyClass.foo.__name__print MyClass.foo.__doc__printprint MyClass.__dict__['foo']print MyClass.__dict__['foo'].__name__print MyClass.__dict__['foo'].__doc__

result

<unbound method MyClass.foo>fooI am the docstring of foo<function foo at 0x011C27B0>fooI am the docstring of foo