On 15 September 2011 16:17, Ian Kelly <ian.g.ke...@gmail.com> wrote: > On Thu, Sep 15, 2011 at 5:10 AM, Arnaud Delobelle <arno...@gmail.com> wrote: >> Hi all, >> >> You can do: >> >> def foo(): >> "foodoc" >> pass >> >> 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. [...] > > 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. >
Yes it seems to be the case. Even if None is not used in the function, it appears in co_consts. Good thinking! Thanks, -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list