[Python-Dev] Best way to specify docstrings for member objects

Serhiy Storchaka storchaka at gmail.com
Wed Mar 20 03:41:34 EDT 2019


19.03.19 20:55, Raymond Hettinger пише:
> I'm working on ways to make improve help() by giving docstrings to member objects.
> 
> One way to do it is to wait until after the class definition and then make individual, direct assignments to __doc__ attributes.This way widely the separates docstrings from their initial __slots__ definition.   Working downstream from the class definition feels awkward and doesn't look pretty.
> 
> There's another way I would like to propose¹.  The __slots__ definition already works with any iterable including a dictionary (the dict values are ignored), so we could use the values for the  docstrings.

I think it would be nice to separate docstrings from the bytecode. This 
would be allow to have several translated sets of docstrings and load an 
appropriate set depending on user preferences. This would help in 
teaching Python.

It is possible with docstrings of modules, classes, functions, methods 
and properties (created by using the decorator), because the compiler 
knows what string literal is a docstring. But this is impossible with 
namedtuple fields and any of the above ideas for slots.

It would be nice to allow to specify docstrings for slots as for methods 
and properties. Something like in the following pseudocode:

class NormalDist:
     slot mu:
	'''Arithmetic mean'''
     slot sigma:
         '''Standard deviation'''

It would be also nice to annotate slots and add default values (used 
when the slot value was not set).

class NormalDist:
     mu: float = 0.0
	'''Arithmetic mean'''
     sigma: float = 1.0
         '''Standard deviation'''



More information about the Python-Dev mailing list