如何撤回字符串 Docstring 功能 python 来自功能本身?

我想打印函数文档字符串 python 从函数本身的内部。
例如。


def my_function/self/:
"""Doc string for my function."""
# print the Docstring here.


目前我在定义后立即完成
my_function

.


print my_function.__doc__


但更好地让功能使其成为自身。

我试着打电话
print self.__doc__


print self.my_function.__doc__


print this.__doc__

里面 my_function, 但它没有工作。
已邀请:

卫东

赞同来自:

def my_func//:
"""Docstring goes here."""
print my_func.__doc__


只要您没有将对象绑定到名称的对象,它就会起作用
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


你会做的情况,相当罕见,但他们发生了。

但是,如果您编写如下装饰符:


def passmein/func/:
def wrapper/*args, **kwargs/:
return func/func, *args, **kwargs/
return wrapper


现在你可以做到:


@passmein
def my_func/me/:
print me.__doc__


它保证您的功能将收到自己的链接 /相似地
self

/ 作为你的第一个参数,所以它可以总是得到一个字符串 docstring 适当的功能。 当用于通常的方法时
self

成为第二个论点。

君笑尘

赞同来自:

这应该是工作 /在我的测试中,这是真的,结论也包括在内。/. 你可能会使用
__doc__

反而 getdoc, 但我喜欢它,所以这正是我使用的。 此外,它还不要求您了解名称 class/method/function.

类,方法和功能的示例。 告诉我,如果不是你在寻找什么 :/


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

詹大官人

赞同来自:

这项工作:


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

my_function//


在 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//


但是,它本身就无法工作:


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


foo = MyClass//

foo.my_function//


NameError: 全球名称 'my_function' 不是绝对

莫问

赞同来自:

有一种相当简单的方法来做到这一点,没有人提到过:


import inspect

def func//:
"""Doc string"""
print inspect.getdoc/func/


它确实你想要的。

这里没有什么特别的。 一切都发生了 - 这是什么,制作
func.__doc__

在函数中,推迟到足够长的属性的权限以搜索
__doc__

它在你期待时工作。

我用它 docopt 对于点输入控制台方案。

小姐请别说爱

赞同来自:

您将您的问题提出为类方法,而不是作为函数。 这里有重要名称空间。 对于功能
print my_function.__doc__

适合,因为 my_function 位于全球名称空间。

对于类方法,然后
print self.my_method.__doc__

将是去的方式。

如果您不想指定方法的名称,而且将其发送变量,则可以使用内置函数。 hasattr/object,attribute/ 和 getattr/obj, attr/, 正如他们所说的那样,允许您使用作为方法名称的行传输变量。 例如


class MyClass:
def fn/self/:
"""A docstring"""
print self.fn.__doc__

def print_docstrings/object/:
for method in dir/ object /:
if method[:2] == '__': # A protected function
continue
meth = getattr/ object, method /
if hasattr/ meth , '__doc__' /:
print getattr/ meth , '__doc__' /

x = MyClass//
print_docstrings/ x /

董宝中

赞同来自:

正如反复指出的那样,使用函数的名称是目录中的动态搜索 globals//. 它仅适用于定义模块,仅适用于全局函数。 如果你想学习字符串 doc 会员职能,您还需要代表课程找到方法。 - 什么是非常繁琐的,因为这些名字可能很长:


def foo//:
""" this is foo """
doc = foo.__doc__
class Foo:
def bar/self/:
""" this is bar """
doc = Foo.bar.__doc__


相等的


def foo//:
""" this is foo """
doc = globals//["foo"].__doc__
class Foo:
def bar/self/:
""" this is bar """
doc = globals//["Foo"].bar.__doc__


如果你想看到一个字符串 doc 来电者,它仍然不起作用,因为你的打印助手可以生活在一个完全不同的模块中,完全不同的字典 globals//. 唯一的正确选择是调查堆栈框架 , 但 Python 不给您执行对象,它只对代码对象引用 "f_code". 但继续去,因为还有一个链接 "f_globals" 这项特征。 所以你可以写一个功能来获得 doc 以这种方式导致订阅者,以及您将获得自己的字符串 doc.


import inspect

def get_caller_doc//:
frame = inspect.currentframe//.f_back.f_back
for objref in frame.f_globals.values//:
if inspect.isfunction/objref/:
if objref.func_code == frame.f_code:
return objref.__doc__
elif inspect.isclass/objref/:
for name, member in inspect.getmembers/objref/:
if inspect.ismethod/member/:
if member.im_func.func_code == frame.f_code:
return member.__doc__


然后去看看:


def print_doc//:
print get_caller_doc//

def foo//:
""" this is foo """
print_doc//

class Foo:
def bar/self/:
""" this is bar """
print_doc//

def nothing//:
print_doc//

class Nothing:
def nothing/self/:
print_doc//

foo//
Foo//.bar//

nothing//
Nothing//.nothing//

# and my doc

def get_my_doc//:
return get_caller_doc//

def print_my_doc//:
""" showing my doc """
print get_my_doc//

print_my_doc//


导致此输出


this is foo 
this is bar
None
None
showing my doc


事实上,大多数人都想要自己的行 doc 仅作为参数传输,但被称为辅助功能可以搜索自己的。 我在我的毫无间程的代码中使用它,有时可以方便地填充一些日志或线条。 doc 作为测试数据。 这就是呈现的原因 get_caller_doc// 寻找仅全局测试函数和Dick-Class成员函数,但我认为这对想要了解字符串的大多数人来说足够了 doc.


class FooTest/TestCase/:
def get_caller_doc/self/:
# as seen above
def test_extra_stuff/self/:
""" testing extra stuff """
self.createProject/"A"/
def createProject/self, name/:
description = self.get_caller_doc//
self.server.createProject/name, description/


确定正确的一个 get_frame_doc /frame/ 从 sys._getframe/1/ 遗迹 reader//.

帅驴

赞同来自:

尝试:


class MyClass//:
# ...
def my_function/self/:
"""Docstring for my function"""
print MyClass.my_function.__doc__
# ...


/*/ 后
my_function//

没有结肠 /
:

/

奔跑吧少年

赞同来自:

插入
print __doc__

在课堂公告之后,之前立即
def __init__

, 将打印一个字符串 doc 每次启动具有类的对象时,在控制台中

要回复问题请先登录注册