How do I programmatically set the docstring?

前端 未结 6 1878
慢半拍i
慢半拍i 2020-12-01 07:21

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\

6条回答
  •  忘掉有多难
    2020-12-01 07:57

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

提交回复
热议问题