k4otix
k4otix

Reputation: 11

Python set docstring and get method name of dynamically generated classmethod

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?

Upvotes: 1

Views: 1135

Answers (1)

Adam Hughes
Adam Hughes

Reputation: 16309

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

Upvotes: 1

Related Questions