Can a docstring be created dynamically during the function definition?
<p>There is a small problem with the following code:</p>

<pre><code>docs = {}

def register(func):
    docs[func] = func.__doc__
    return func

@register
def blue():
    """Color function."""
    pass

@register
def red():
    pass
red.__doc__ = blue.__doc__

print("docstring real:", red.__doc__)
print("docstring registered:", docs[red]) # None!
</code></pre>

<p>The <code>red</code> function gets its docstring from the <code>blue</code> function. But this happens <strong>after</strong> the decorator registers it.</p>

<p>I could add an optional argument to the <code>register</code> decorator, but this case is <em>not special enough</em>. It looks like I have to copy and paste the string. No problem with that ... except that I'm still wondering if there is an trivial fix that I have overlooked.</p>

<hr>

<p><strong>UPDATE</strong>: After realizing that <code>@decorator</code> is just a syntactic sugar I've found a workaround:</p>

<pre><code># @register
def red():
    pass
red.__doc__ = blue.__doc__
red = register(red)
</code></pre>

<p>This solves my problem.</p>

<p>The docstring is still created after the function definition. For an answer to the original question see the comment added by idjaw.</p>