"help( pi )"

Chris Angelico rosuav at gmail.com
Fri Nov 17 17:50:29 EST 2017


On Sat, Nov 18, 2017 at 9:38 AM, Cameron Simpson <cs at cskk.id.au> wrote:
> I don't think he's arguing that help magicly recognises 3.1415926536 as "pi"
> and produces a docstring for it and all "sufficiently close" values. I'm
> not.  But the math module has bound "pi" to a specific float. Why _can't_ we
> annotate that float with a docstring?
>
>  math.py:
>    pi = 3.1415926536
>    pi.__doc__ = 'The ratio of round things to straight things. Roughly 3.'
>
> Now, I accept that the "CPython coaleases some values to shared singletons"
> thing is an issue, but the language doesn't require it, and one could change
> implementations such that applying a docstring to an object _removed_ it
> from the magic-shared-singleton pool, avoiding conflicts with other uses of
> the same value by coincidence.
>

Perhaps what we want is not so much "attach docstrings to floats" but
"get documentation for a module attribute, not for the object referred
to".

pi = 3.1415926536 # A yet-to-be-released version of TeX

help("math.pi")
==>
"""
math.pi [3.1415926536]

A yet-to-be-released version of TeX
"""

To make this work, the help() function would have to be given a
string, or else be some sort of magic UI feature rather than a plain
function. It'd then also need to get hold of either the source code or
the compiled math.pyc (but not the importable module) and somehow
locate the module attribute assignment operation. It wouldn't be
efficient, but it would be something that's only done when a human
specifically asks for it, so taking 50ms isn't going to mean much.

Possible? Yes. Practical? Not so sure.

ChrisA



More information about the Python-list mailing list