如何从函数本身内部打印python函数的Docstring?

sha*_*e87 61 python printing docstring function

我想从函数本身内部打印python函数的docstring.例如.

def my_function(self):
  """Doc string for my function."""
  # print the Docstring here.
Run Code Online (Sandbox Code Playgroud)

目前我my_function在定义之后直接这样做.

print my_function.__doc__
Run Code Online (Sandbox Code Playgroud)

但宁愿让函数自己这样做.

我已经打过电话print self.__doc__ print self.my_function.__doc__print this.__doc__内部创建my_function但这并没有工作.

kin*_*all 67

def my_func():
    """Docstring goes here."""
    print my_func.__doc__
Run Code Online (Sandbox Code Playgroud)

只要您不更改绑定到名称的对象,这将起作用my_func.

new_func_name = my_func
my_func = None

new_func_name()
# doesn't print anything because my_func is None and None has no docstring
Run Code Online (Sandbox Code Playgroud)

你这样做的情况相当罕见,但它们确实发生了.

但是,如果你像这样写一个装饰器:

def passmein(func):
    def wrapper(*args, **kwargs):
        return func(func, *args, **kwargs)
    return wrapper
Run Code Online (Sandbox Code Playgroud)

现在你可以这样做:

@passmein
def my_func(me):
    print me.__doc__
Run Code Online (Sandbox Code Playgroud)

这将确保您的函数获得对其自身(类似于self)的引用作为其第一个参数,因此它始终可以获得正确函数的文档字符串.如果在方法上使用,通常self会成为第二个参数.

  • 我真的很喜欢你的装饰方法.与帧检查相比,pythonic和风险更低,并且允许您避免使用函数名称.光滑!Upvoted! (7认同)

Teh*_*nix 8

这应该工作(在我的测试中,它也包括输出).您可以使用__doc__而不是getdoc,但我喜欢它,所以这就是我使用的.此外,这不需要您知道类/方法/函数的名称.

类,方法和函数的示例.告诉我,如果它不是你想要的:)

from inspect import *

class MySelfExplaningClass:
    """This is my class document string"""

    def __init__(self):
        print getdoc(self)

    def my_selfexplaining_method(self):
        """This is my method document string"""
        print getdoc(getattr(self, getframeinfo(currentframe()).function))


explain = MySelfExplaningClass()

# Output: This is my class document string

explain.my_selfexplaining_method()

# Output: This is my method document string

def my_selfexplaining_function():
    """This is my function document string"""
    print getdoc(globals()[getframeinfo(currentframe()).function])

my_selfexplaining_function()

# Output: This is my function document string
Run Code Online (Sandbox Code Playgroud)


Kei*_*han 7

有一个非常简单的方法可以做到这一点,但还没有人提到:

import inspect

def func():
    """Doc string"""
    print inspect.getdoc(func)
Run Code Online (Sandbox Code Playgroud)

这就是你想要的。

这里没有什么奇怪的事情发生。所发生的一切是,通过func.__doc__在函数中执行操作将属性解析推迟足够长的时间,以便查找__doc__它可以按您的预期工作。

我将其与 docopt 一起用于控制台脚本入口点。

  • 但它并不比仅仅 `print func.__doc__` 好 (5认同)

jgr*_*tty 6

这有效:

def my_function():
  """Docstring for my function"""
  #print the Docstring here.
  print my_function.__doc__

my_function()
Run Code Online (Sandbox Code Playgroud)

在Python 2.7.1中

这也适用:

class MyClass(object):
    def my_function(self):
        """Docstring for my function"""
        #print the Docstring here, either way works.
        print MyClass.my_function.__doc__
        print self.my_function.__doc__


foo = MyClass()

foo.my_function()
Run Code Online (Sandbox Code Playgroud)

但是,这不能单独工作:

class MyClass(object):
    def my_function(self):
        """Docstring for my function"""
        #print the Docstring here.
        print my_function.__doc__


foo = MyClass()

foo.my_function()
Run Code Online (Sandbox Code Playgroud)

NameError:全局名称“ my_function”未定义