Python set docstring and get method name of dynamically generated classmethod

一笑奈何 提交于 2020-01-14 03:28:26

问题


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()

Currently the output is:

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

What I'd like to have is:

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

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.

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?


回答1:


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")

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'}


来源:https://stackoverflow.com/questions/26145076/python-set-docstring-and-get-method-name-of-dynamically-generated-classmethod

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!