How do I programmatically set the docstring?
<p>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 <code>__doc__</code> I'd do the following:</p>

<pre><code>def wrapper(a):
    def add_something(b):
       return a + b
    add_something.__doc__ = 'Adds ' + str(a) + ' to `b`'
    return add_something
</code></pre>

<p>Then I could do</p>

<pre><code>&gt;&gt;&gt; add_three = wrapper(3)
&gt;&gt;&gt; add_three.__doc__
'Adds 3 to `b`
</code></pre>

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

<hr>

<p>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 <code>__doc__</code> is writeable in my case it isn't.</p>

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

<pre><code>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 &gt; 0)

    return ATest
</code></pre>

<p>If I create this class and try to set the docstring of <code>testSomething</code> I get an error:</p>

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