You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Documentation from inspect.getdoc() for non-callable attributes math.e can be misleading. #135316
inspect module returns wrong documentation for math.e.
print(inspect.getdoc(math.e))
# Returns: # "Convert a string or number to a floating point number, if possible."
This should return something to the effect of:
"""
Returns the nearest representable value to the mathematical constant e, which has a value of 2.718281..." and is the base of natural logorithms.
"""
In theory, we could create a separate float subclass for each constant (e, inf, etc.) that provides a specific docstring. But I don't think it's worth it, nor worth encouraging.
changed the title [-]documentation from inspect.getdoc() for math.e is incorrect[/-][+]Documentation from inspect.getdoc() for non-callable attributes math.e can be misleading.[/+]on Jun 10, 2025
The issue with getdoc() is essentially the same as for help(). Only modules, classes, and functions have individual docstrings. dir(math.e) equals dir(float), with equal __doc__s, and the same is true for every float.
Admittedly while I've been working on the project that triggered this, I keep forgetting that math.e isn't a function. The explanations given make sense in that context. I do wonder, though, if it would be worth using a function that returns e, or tau, or pi, or whatever other constants, instead of just having the constant. Would there be unnecessary performance impact? I don't know, but in my opinion, it would be helpful to have a useful docstring about these constants.
Yeah, it would be slower and more importantly, it would break a lot of code. I think a better approach would be to design a way to generally add docstrings to arbitrary objects (such as a global __docs__ dict). But there isn't any active proposal to do that as far as I'm aware.
Another possibility would be introducing module-level property, but that would be just as slow as a regular function call. On the plus (?) side, it could also prevent redefining fundamental constants :)
Python has probably 100s of named constants. Most? are more obscure than math.e. A generic doc method would be interesting to look at. But note that sometimes the class doc is what a user wants or needs.
Names of predefined constants should be in the index. 'e ... math module' leads to "e The mathematical constant e = 2.718281…, to available precision."
Normal (not slotted) wuer classes can include passing a docstring on creation:
>>> class D:
... def __init__(self, doc):
... self.__doc__ = doc
...
>>> help(D('d object'))
Help on D in module __main__:
<__main__.D object>
d object
Or '.doc can be added later. Builtin class instances often have no instance dict and hence no specific instance attributes.
Activity
zware commentedon Jun 9, 2025
math.eis just afloatinstance;inspect.getdoccan only return data about thefloattype from it. I'm afraid there's nothing we can do here.ZeroIntensity commentedon Jun 10, 2025
In theory, we could create a separate
floatsubclass for each constant (e,inf, etc.) that provides a specific docstring. But I don't think it's worth it, nor worth encouraging.[-]documentation from inspect.getdoc() for math.e is incorrect[/-][+]Documentation from inspect.getdoc() for non-callable attributes math.e can be misleading.[/+]help(math.pi)#104042terryjreedy commentedon Jun 10, 2025
The issue with getdoc() is essentially the same as for help(). Only modules, classes, and functions have individual docstrings. dir(math.e) equals dir(float), with equal
__doc__s, and the same is true for every float.astrolamb-gaming commentedon Jun 10, 2025
Admittedly while I've been working on the project that triggered this, I keep forgetting that math.e isn't a function. The explanations given make sense in that context. I do wonder, though, if it would be worth using a function that returns e, or tau, or pi, or whatever other constants, instead of just having the constant. Would there be unnecessary performance impact? I don't know, but in my opinion, it would be helpful to have a useful docstring about these constants.
ZeroIntensity commentedon Jun 10, 2025
Yeah, it would be slower and more importantly, it would break a lot of code. I think a better approach would be to design a way to generally add docstrings to arbitrary objects (such as a global
__docs__dict). But there isn't any active proposal to do that as far as I'm aware.zware commentedon Jun 10, 2025
Another possibility would be introducing module-level
property, but that would be just as slow as a regular function call. On the plus (?) side, it could also prevent redefining fundamental constants :)terryjreedy commentedon Jun 11, 2025
Python has probably 100s of named constants. Most? are more obscure than math.e. A generic doc method would be interesting to look at. But note that sometimes the class doc is what a user wants or needs.
Names of predefined constants should be in the index. 'e ... math module' leads to "e The mathematical constant e = 2.718281…, to available precision."
Normal (not slotted) wuer classes can include passing a docstring on creation:
Or '.doc can be added later. Builtin class instances often have no instance dict and hence no specific instance attributes.
skirpichev commentedon Jun 11, 2025
Though, we can use float subclasses:
Edit:
Here is a direct implementation of above:
But not without a price:
In the master:
Maybe this speed difference can be mitigated with a static subclass, that will have a special getter for
__doc__?a patch