Python设置docstring并获取动态生成的类方法的方法名称 [英] Python set docstring and get method name of dynamically generated classmethod

查看:225
本文介绍了Python设置docstring并获取动态生成的类方法的方法名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图按以下方式获取/设置动态创建的类方法的名称和文档字符串,但在弄清楚确切的操作方法上遇到了麻烦:

I'm trying to get/set the name and docstring of dynamically created class methods as follows, but am having trouble figuring out exactly how to do it:

import sys
import inspect

class test(object):
    pass

@classmethod
def genericFunc(cls, **kwargs):
    print "function:", (inspect.stack()[0][3])
    print "kwargs:", kwargs

function_list = ['myF1', 'myF2']
for func in function_list:
    setattr(test, func, genericFunc)
    #set docstring for func here?

if __name__ == '__main__':
    x = test()
    print "docstring:", x.myF1.__doc__
    x.myF1(arg1="foo")
    y = test()
    print "docstring:", y.myF2.__doc__
    y.myF2(arg1="foo", arg2="bar")
    sys.exit()

当前输出为:

docstring: None
function: genericFunc
kwargs: {'arg1': 'foo'}
docstring: None
function: genericFunc
kwargs: {'arg1': 'foo', 'arg2': 'bar'}

我想拥有的是:

docstring: description of myF1
function: myF1
kwargs: {'arg1': 'foo'}
docstring: description of myF2
function: myF2
kwargs: {'arg1': 'foo', 'arg2': 'bar'}

在for循环中,我尝试执行 setattr(test.func , __ doc __,%s的描述%func),从而导致AttributeError异常(类型对象 test没有属性 func), nd如果我硬编码'test.myF1'而不是'test.func',则会因'instancemethod'的属性'__ doc __'不可写而得到AttributeError。

In the for loop I tried doing setattr(test.func, "__doc__", "description of %s" % func), which resulted in an AttributeError exception (type object 'test' has no attribute 'func'), and if I hard-code 'test.myF1' instead of 'test.func' I get an AttributeError for attribute '__doc__' of 'instancemethod' not being writable.

最后,inspect.stack()返回 genericFunc而不是动态函数名( myF1或 myF2),是否有可能获取/设置后者这样我就可以检查genericFunc中方法的 __ name __ 的值,以根据其值执行某些操作?

Finally, the inspect.stack() returns "genericFunc" instead of a dynamic function name ('myF1' or 'myF2'), is it possible to get/set the latter so that I can check the value of the method's __name__ within genericFunc to perform certain actions depending on its value?

推荐答案

您的通用函数本身没有文档字符串。当我添加文档字符串时,这对我来说工作正常:

Your generic function itself doesn't have a docstring. This works fine for me when I add a docstring:

    import inspect

    class test(object):
        pass

    @classmethod
    def genericFunc(cls, **kwargs):
        """ I'm a docstring"""
        print "function:", (inspect.stack()[0][3])
        print "kwargs:", kwargs

    function_list = ['myF1', 'myF2']
    for func in function_list:
        setattr(test, func, genericFunc)
        #set docstring for func here?

    if __name__ == '__main__':
        x = test()
        print "docstring:", x.myF1.__doc__
        x.myF1(arg1="foo")
        y = test()
        print "docstring:", y.myF2.__doc__
        y.myF2(arg1="foo", arg2="bar")

此外,为什么最后要使用sys.exit()?结果,我得到:

Also, why sys.exit() at the end? As a result, I get:

docstring:  I'm a docstring
function: genericFunc
kwargs: {'arg1': 'foo'}
docstring:  I'm a docstring
function: genericFunc
kwargs: {'arg1': 'foo', 'arg2': 'bar'}

这篇关于Python设置docstring并获取动态生成的类方法的方法名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆