以下行为之间的区别是什么:func:和:meth:Python Sphinx中的角色?

时间:2016-01-18 06:10:34

标签: python python-sphinx

http://www.sphinx-doc.org/en/stable/domains.html#cross-referencing-python-objects处的Sphinx文档说明了

  

:py:func:引用Python函数;可以使用点名。角色文本不需要包括尾随括号以增强可读性;   它们将由Sphinx自动添加   add_function_parentheses配置值为True(默认值)。

     

:PY:甲基:   引用对象的方法。角色文本可以包括类型名称和方法名称;如果它出现在描述中   一个类型,类型名称可以省略。可以使用带点的名称。

但我发现他们的行为方式没有任何区别。

这是我为其生成文档的Python模块。

"""foo module."""

def hello(name):
    """Print hello addressed to *name*.

    Args:
      name (str): Name to address.
    """
    print('hello', name)

class Foo:

    """Foo class."""

    def bye(self, name):
        """Print bye addressed to *name*.

        Args:
          name (str): Name to address.
        """
        print('bye', name)

if __name__ == '__main__':
    hello('world')
    Foo().bye('python')

这就是我在index.rst文件中的内容。

Foo Documentation
=================

See :func:`foo.hello` and :func:`foo.Foo.bye`.

Also, see :meth:`foo.hello` and :meth:`foo.Foo.bye`.

foo module
==========
.. automodule:: foo
    :members:

执行make html后,这是我看到的输出。

Screeshot of foo documentation

无论目标是函数还是方法,:func::meth:角色都生成了对helloFoo.bye的有效交叉引用超链接。

:func::meth:角色之间的区别是什么?你能提供一个他们表现不同的例子吗?

3 个答案:

答案 0 :(得分:10)

我看过Sphinx代码。我能够辨别的唯一区别是每个角色都生成HTML元素,其HTML class包含创建它的角色的名称。例如,code角色的:func:元素将如下所示:

<code class="xref py py-func docutils literal">

对于:meth:角色,它会py-meth而不是py-func。 Sphinx附带的库存CSS样式不区分py-methpy-func,但可以使用样式表来对它们进行不同的样式化。

对于踢,我尝试过其他角色(例如class)并让它们指向对象上的方法。即使没有任何意义,狮身人面像也没有问题。

答案 1 :(得分:0)

在生成的索引中使用的语义信息例如将某事物标记为函数或方法。正如路易斯已经提到的,可以通过CSS在HTML中对它们进行不同的样式设计。

答案 2 :(得分:0)

在功能方面至少有一个区别。

每当您使用自动类语法(类名前面的.)时,都会自动解析完整的类名:

  • :meth:`.myClass`将搜索范围限制为当前模块。
  • :func:`.myClass`也解析外部类。
相关问题