[Python-ideas] Dynamic getting of __doc__ of a function

Steven D'Aprano steve at pearwood.info
Sun Oct 7 04:31:01 EDT 2018

On Sun, Oct 07, 2018 at 09:25:13AM +0200, Marko Ristin-Kaufmann wrote:
> Hi,
> I'm working on decorators that have a dynamic __doc__ property computed at
> runtime and not at decoration time.

You mean like this?

py> class X:
...     @property
...     def __doc__(self):
...         return "NOBODY expects the %s Inquisition!" % self.nationality
...     def __init__(self, nationality):
...         self.nationality = nationality
py> a = X("Spanish")
py> b = X("Belgian")
py> a.__doc__
'NOBODY expects the Spanish Inquisition!'
py> b.__doc__
'NOBODY expects the Belgian Inquisition!'

The only downside is that help(a) doesn't do the right thing. I believe 
that counts as a bug in help().

> The decorator must return the wrapper as a function and can not return a
> callable object with __call__ since the "self" argument would not be
> properly passed through the decorator.

Sorry, I don't get this. Can you demonstrate? Code speaks much louder 
than words.


More information about the Python-ideas mailing list