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

Jonathan Fine jfine2358 at gmail.com
Sun Oct 7 11:36:52 EDT 2018


Hi Marko

You wrote:

> I just couldn't figure out how to make the __doc__ attribute of a function a getter. Is this a bug or am I making a mistake? If it's my mistake, is there any other way how to dynamically __doc__ a function or am I forced to set __doc__ of a function at the decoration time?

Thank you for your clear examples. I'm not sure, but I may have found
the problem.

As I recall, setting obj.attr to a property won't work. Instead, you
have to set type(obj).attr to the property. And now you come up
against a problem. You can't set __doc__ on the type 'function', nor
can you subclass the type 'function'.

>>> def fn(): pass
>>> type(fn)
<class 'function'>

>>> type(fn).__doc__
'function(code, globals[, name[, argdefs[, closure]]]) [snip]'

>>> type(fn).__doc__ = None
TypeError: can't set attributes of built-in/extension type 'function'

>>> class FN(type(fn)): pass
TypeError: type 'function' is not an acceptable base type

As I (more vaguely) recall, something was done recently to add a get
attribute property that works directly on objects, rather than on
instances of a class. But I've not been able to find it. Perhaps I
misunderstood PEP 562.

Some perhaps relevant URLs:
https://www.python.org/dev/peps/pep-0224/ Attribute Docstrings
https://www.python.org/dev/peps/pep-0549/ Instance Descriptors
https://www.python.org/dev/peps/pep-0562/ Module __getattr__ and __dir__
https://stackoverflow.com/questions/2447353/getattr-on-a-module

-- 
Jonathan


More information about the Python-ideas mailing list