Why does dynamic doc string do not work?

Ian Kelly ian.g.kelly at gmail.com
Tue Aug 21 13:21:10 EDT 2012


On Tue, Aug 21, 2012 at 11:09 AM, Ian Kelly <ian.g.kelly at gmail.com> wrote:
> On Tue, Aug 21, 2012 at 10:44 AM, Miki Tebeka <miki.tebeka at gmail.com> wrote:
>> Greetings,
>>
>>>>> class A:
>> ...     '''a doc string'''
>> ...
>>>>> A.__doc__
>> 'a doc string'
>>>>> class B:
>> ...     '''a {} string'''.format('doc')
>> ...
>>>>> B.__doc__
>>>>>
>>
>> Is there's a reason for this?
>>
>> I know I can do:
>>>>> class B:
>> ...    __doc__ = '''a {} string'''.format('doc')
>>
>> And it'll work, but I wonder why the first B docstring is empty.
>
> The docstring is built at compile-time, not at run-time, so it must be
> a literal, not an arbitrary expression.

Also, if it did allow arbitrary expressions, then the syntax would be ambiguous.

def a():
    foo()
    do_stuff()

Is "foo()" intended to return a doc string?  If so, then it should be
called when the function object is built, not when the function is
called.  On the other hand, maybe it's intended to be part of the
function's code, in which case it should be called only when the
function itself is called.



More information about the Python-list mailing list