How does a function know the docstring of its code object?
ian.g.kelly at gmail.com
Thu Sep 15 11:17:53 EDT 2011
On Thu, Sep 15, 2011 at 5:10 AM, Arnaud Delobelle <arnodel at gmail.com> wrote:
> Hi all,
> You can do:
> def foo():
> function = type(lambda:0)
> foo2 = function(foo.__code__, globals())
> assert foo2.__doc__ == "foodoc"
> I am wondering how the function constructor knows that foo.__code__
> has a docstring. I can see that
> foo.__code__.co_consts == ('foodoc',)
> But I can't find where in foo.__code__ is stored the information that
> the first constant in foo.__code__ is actually a docstring.
>From what I'm seeing, it appears that if there is no docstring, the
first constant will always be None. So if the first constant is a
string, then it's a docstring.
More information about the Python-list