Why does dynamic doc string do not work?
Steven D'Aprano
steve+comp.lang.python at pearwood.info
Tue Aug 21 13:22:24 EDT 2012
On Tue, 21 Aug 2012 09:44:10 -0700, Miki Tebeka 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?
Yes. Python only generates docstrings automatically from string literals,
not arbitrary expressions.
Arbitrary expressions are evaluated and then garbage collected, so:
class B:
'''a {} string'''.format('doc')
is equivalent to:
class B:
__doc__ = None
_tmp = '''a {} string'''.format('doc')
del _tmp
except that the name "_tmp" doesn't actually get used.
--
Steven
More information about the Python-list
mailing list