链接到 python 文档字符串中的类方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21289806/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Link to class method in python docstring
提问by saroele
I want to add a link to a method in my class from within the docstring of another method of the same class. I want the link to work in sphinx and preferentially also in Spyder and other Python IDE's.
我想从同一个类的另一个方法的文档字符串中添加一个链接到我的类中的一个方法。我希望链接在 sphinx 中工作,并且优先在 Spyder 和其他 Python IDE 中工作。
I tried several options and found only one that works, but it's cumbersome.
我尝试了几种选择,发现只有一种有效,但很麻烦。
Suppose the following structure in mymodule.py
假设以下结构 mymodule.py
def class MyClass():
def foo(self):
print 'foo'
def bar(self):
"""This method does the same as <link to foo>"""
print 'foo'
I tried the following options for <link to foo>:
我尝试了以下选项<link to foo>:
- :func:`foo`
- :func:`self.foo`
- :func:`MyClass.foo`
- :func:`mymodule.MyClass.foo`
- :func:`foo`
- :func:`self.foo`
- :func:`MyClass.foo`
- :func:`mymodule.MyClass.foo`
The only one that effectively produces a link is :func:`mymodule.MyClass.foo`, but the link is shown as mymodule.MyClass.foo()and I want a link that is shown as foo()or foo.
None of the options above produces a link in Spyder.
唯一有效生成链接的是:func:`mymodule.MyClass.foo`,但链接显示为mymodule.MyClass.foo(),我想要一个显示为foo()or的链接foo。
上述选项都不会在 Spyder 中生成链接。
Thanks for your help.
谢谢你的帮助。
采纳答案by saroele
The solution that works for Sphinx is to prefix the reference with ~.
适用于 Sphinx 的解决方案是在引用前加上~.
Per the Sphinx documentation on Cross-referencing Syntax,
根据关于交叉引用语法的 Sphinx 文档,
If you prefix the content with ~, the link text will only be the last component of the target. For example, :py:meth:
~Queue.Queue.getwill refer to Queue.Queue.get but only display get as the link text.
如果您为内容添加前缀 ~,则链接文本将仅是目标的最后一个组件。例如, :py:meth:
~Queue.Queue.get将引用 Queue.Queue.get 但只显示 get 作为链接文本。
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.
这会导致 html 看起来像这样 : This method does the same as foo(),并且 foo()是一个链接。
However, note that this may not display in Spyder as a link.
但是,请注意,这可能不会在 Spyder 中显示为链接。
回答by eyquem
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
在我看来,您只需要在表达式中添加__name__或__doc__即可获得您想要的东西。
我仍然不确定是否正确理解了目标
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'
print
print MyClass.foo
print MyClass.foo.__name__
print MyClass.foo.__doc__
print
print MyClass.__dict__['foo']
print MyClass.__dict__['foo'].__name__
print MyClass.__dict__['foo'].__doc__
result
结果
<unbound method MyClass.foo>
foo
I am the docstring of foo
<function foo at 0x011C27B0>
foo
I am the docstring of foo
回答by devin_s
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.
有关更多信息,请查看交叉引用 Python 对象。

