且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何以编程方式设置文档字符串?

更新时间:2023-02-12 19:23:38

我会将文档字符串传递给工厂函数并使用 type 手动构造类.

def make_testcase(filename, myfunc, docstring):def test_something(self):数据 = loadmat(文件名)结果 = myfunc(数据)self.assertTrue(结果> 0)clsdict = {'test_something': test_something,'__doc__':文档字符串}返回类型('ATest',(unittest.TestCase,),clsdict)MyTest = makeTestCase('some_filename', my_func, '这是一个文档字符串')

I have a wrapper function that returns a function. Is there a way to programmatically set the docstring of the returned function? If I could write to __doc__ I'd do the following:

def wrapper(a):
    def add_something(b):
       return a + b
    add_something.__doc__ = 'Adds ' + str(a) + ' to `b`'
    return add_something

Then I could do

>>> add_three = wrapper(3)
>>> add_three.__doc__
'Adds 3 to `b`

However, since __doc__ is read-only, I can't do that. What's the correct way?


Edit: Ok, I wanted to keep this simple, but of course this is not what I'm actually trying to do. Even though in general __doc__ is writeable in my case it isn't.

I am trying to create testcases for unittest automatically. I have a wrapper function that creates a class object that is a subclass of unittest.TestCase:

import unittest
def makeTestCase(filename, my_func):
    class ATest(unittest.TestCase):
        def testSomething(self):
            # Running test in here with data in filename and function my_func
            data  = loadmat(filename)
            result = my_func(data)
            self.assertTrue(result > 0)

    return ATest

If I create this class and try to set the docstring of testSomething I get an error:

>>> def my_func(): pass
>>> MyTest = makeTestCase('some_filename', my_func)
>>> MyTest.testSomething.__doc__ = 'This should be my docstring'
AttributeError: attribute '__doc__' of 'instancemethod' objects is not writable

I would pass the docstring into the factory function and use type to manually construct the class.

def make_testcase(filename, myfunc, docstring):
    def test_something(self):
        data = loadmat(filename)
        result = myfunc(data)
        self.assertTrue(result > 0)

    clsdict = {'test_something': test_something,
               '__doc__': docstring}
    return type('ATest', (unittest.TestCase,), clsdict)

MyTest = makeTestCase('some_filename', my_func, 'This is a docstring')