Closed as not planned
Labels
stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
Description
Documentation
Both print(math.pi.__doc__) and print(help(math.pi)) are returning the wrong documentation.
(A clear and concise description of the issue.)

This code was executed in Thonny 4.02, Python version 3.10.9.
**Convert a string or number to a floating point number, if possible.
Help on float object:
class float(object)
| float(x=0, /)
|
| Convert a string or number to a floating point number, if possible.**
Metadata
Metadata
Assignees
Labels
stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
Activity
ronaldoussoren commentedon May 1, 2023
It might be possible to make this more clear, but this is expected behaviour.
Help on an instance (such as the floating point number
math.pi) showsHelp on ... objectfollowed by the help of the class of the object. This makes it easier to show which methods and members are present on an object by way of its class.AlexWaygood commentedon May 1, 2023
math.piis just an instance offloat, and the__doc__attribute here is providing accurate documentation about thefloatclass in Python. I don't see any way that the behavior can be improved here; it's not possible to set custom docstrings on instances offloat.[-]pi __doc__[/-][+]Confusing output from `help(math.pi)`[/+]ronaldoussoren commentedon May 1, 2023
It might be possible to improve on the generic header to make it clearer that most of the shown help is on the class of the instance. That said, I don't have a proposal and for me the documentation is clear enough.
mfriedenthal commentedon May 1, 2023
mfriedenthal commentedon May 1, 2023
AlexWaygood commentedon May 1, 2023
Yes, the difference is that
math.sqrtis a function object, and it's possible to set custom__doc__attributes on each separate function object. It's not possible to set custom docstrings on each separatefloatobject in the same way. Changing that would be a major change that might have a performance penalty.terryjreedy commentedon May 1, 2023
pi is just a name for the float 3.141592653589793. It happens to be pre-bound in math for convenience. It is otherwise nothing special.
help(pi)is the same ashelp(3.141592653589793)and indeed, the same as help(0.0) or help(394829038029.98989).help` says what one can do with an object, including what attributes one can access, and AFAIK this is the same for all floats.help(math)lists first the method attributes and then, after 300 lines, the data attributes.I believe that both lists are automatically generated by pydoc by getting all attributes and classifying them by a rule.
mfriedenthal commentedon May 1, 2023
terryjreedy commentedon May 1, 2023
More helpful in what way? Help receives a binary floating point object that happens to have the decimal representation '3.141592653589793'. It does not know what name(s), if any, have been bound to the object it receives.
helpis not meant to substitute for reading docs. It is just a reminder.AlexWaygood commentedon May 1, 2023
The output from
help()here might be easier to understand for a beginner here if, instead of "Help on float object", the title said "Help on 3.141592653589793, an instance of float"serhiy-storchaka commentedon May 1, 2024
It would be misleading, because what you get is actually the help on float object, not on 3.141592653589793.
I do not think that we should do anything here and propose to close this issue.
terryjreedy commentedon May 1, 2024
OP claims that it 'confusing', 'wrong', and 'useless' for
help(pi)to return help on class float, but never said whether the claim only applies to 'pi' or to instance names in general, nor what specific or general alternative would be 'better'. We can and have dismissed 'wrong' and 'useless': help should return something different from other object information functions, such as str, repr, type, dir, vars, etc.I considered whether the specific response "Help on x object:" should be reworded. After trying various types of objects, I think not, or at least not only this response. When anything other than a non-empty string is passed, the initial line is consistently "Help on :". I think this consistency is good and any discussion of (consistently) changing it, and possibly breaking code, should be a new issue divorced from this 'math.pi'discussion.
skirpichev commentedon May 27, 2026
I don't think so. It's easy to create a custom float subclass with a different repr and docstring. In fact, we do something like this in the
remodule (_NamedIntConstant).Hmm, I doubt that someone rely on the fact that
pi.__doc__has some generic value ;-) Hardly changing it's repr will also affect something, though it's more disputable.Assuming proposed above solution, performance penalty will come with creation of the module constant. This will affect import timings and so on, not using of constants. IMO, it's not a high price.
He did:
Apparently, OP want documentation on the module constant, like
math.pi. His wishes seems reasonable for me: sometimes you can guess what's it is, just from name, but what aboutmath.tau, for example? Generic documentation for the float class seems at least "useless" in such cases, indeed.Of course, you always can look on the sphinx docs for the module. But if it's not optional - this seems as a limitation for me.
@terryjreedy, can we reopen issue on this ground?
serhiy-storchaka commentedon May 27, 2026
Many code has special fast path for exact float, so using a float subclass will have performance impact. Some code (like marshal) only works with exact floats, so this can be a breaking change. Mixed type binary operations with float subclasses are tricky, this can lead to difficult bugs.
_NamedIntConstantin theremodule does not represent numeric constants, it represents cheap enums, which are also ints. The only reason of using a_NamedIntConstantinstead of a raw int is for internal debugging of theremodule.And I think that changing the class of
math.piis moving in wrong direction. It is a float, it should have the same class like other floats, likeatan(1)andexp(1). You can makehelp('math.pi')showing the specialized help, but don't change the class ofmath.pi.skirpichev commentedon May 28, 2026
That discussed in the d.p.o thread: https://discuss.python.org/t/107540. I doubt it will have a big performance effect.
Any other example than marshal?
d.p.o thread has proposal for such mechanism:
https://discuss.python.org/t/add-a-mixin-class-to-allow-module-constants-with-custom-repr-and-docstrings/107540/2
Though, this doesn't affect repr. Currently, it's impossible to use provided constant as default values in production code:
will produce unreadable
foo(x=2.718281828459045)signature. Maybe we should follow to proposal in #61005 and preserve entered by programmer expression (math.eor0o666or2**63-1)?