-
-
Notifications
You must be signed in to change notification settings - Fork 32k
documenting objects #79630
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
On multiple occasions I have wanted to add documentation not only to Python classes and functions, but also instance variables. This seems to involve (at least) two orthogonal questions:
I have attempted to work around 1) in my custom code by explicitly setting an object's Am I missing something here, i.e. am I approaching the problem the wrong way, or am I the first to want to use object-specific documentation ? |
Minor note on terminology: classes and functions are themselves objects. I think that help() (or in particular PyDoc in general) should support any instance with a __doc__ attribute. Its failure to do so is causing pain, see bpo-12154. |
Exactly ! I'm fully aware of the ubiquity of objects in Python, and it is for that reason that I had naively expected OK, assuming that this is a recognized bug / limitation, it seems easy to address. Is there any discussion concerning what syntax might be used for docstrings associated with objects ? (There seem to be some partial solutions added on top of the Python parser (I think |
I don't know about PyDoc in general, but I would expect help(obj) to
Are you suggesting we need new syntax to automatically assign docstrings I expect that if you want to set a custom instance docstring, you would |
On 2018-12-09 18:35, Steven D'Aprano wrote:
That's why I distinguished between points 1) and 2) in my original mail: I now understand that the current
No, I'm not suggesting that. I'm suggesting that within the current
might be one convention to add a docstring to a variable.
might be another. None of this is syntactically new, but the construction of the AST from It would be great to establish a convention for this, so in the future |
There was a related proposal in https://www.python.org/dev/peps/pep-0258/#attribute-docstrings |
On 2018-12-09 19:48, Karthikeyan Singaravelan wrote:
Right, but that was rejected (for unrelated reasons). The idea itself It's sad, as right now there doesn't appear to be any way to address Stefan --
|
I asked:
Stefan replied:
And then immediately went on to suggest new syntax for automatically Whether you want to call it "new semantics for existing syntax" or "new
To my mind, it makes sense to have dedicated docstring syntax for But for the rare(?) cases of wanting to add docstrings to arbitrary instance.__doc__ = """This is the instance docstring.""" I strongly oppose any suggestion that *comments* be treated as code:
Deleting comments shouldn't make have any runtime effect on the code, So I think there are three related but separate issues here:
|
ad 3) sorry, I picked a bad example - I didn't mean to suggest that immutable objects should in fact become mutable by modifying their ad 1) good, glad to hear that. ad 2) fine. In fact, I'm not even proposing that per-instance docstring generation should be "on" by default. I'm merely asking whether the Python community can't (or even shouldn't) agree on a single convention for how to represent them, such that special tools can then support them, rather than different tools supporting different syntax / conventions. |
The reason that modules, classes, and functions need a special rule for assigning the .__doc__ attribute is that one cannot get a reference to the module, class, or function within the body of its definition. And putting the docstring at the top of a file or after a header is usually the best place. For modules, the body is the file, leaving nowhere else to put the docstring. For classes and functions, the alternative of an assignment elsewhere, after the object is created, remains.
>>> f.__doc__ = 'Docstring outside f body'
>>> help(f)
Help on function f in module __main__: f()
Docstring outside f body This alternative is used in functools.partial and decorators that wrap functions with a function and then copy the original docstring to the wrapper. I think object.doc = 'docstring' is sufficient for other objects and that PEP-224 was wrong to propose otherwise and should have been rejected. So I think that this issue should propose what Steven said: pydoc/help() should be simplified to fetch object.__doc__ with normal lookup, instead of bypassing object if not one of the special types. Stefan Seefeld, can you try patching pydoc to do this? If one wants to add docstrings to builtins, a subclass can work.
>>> i,j = Docint(1), Docint(2)
>>> i+j
3
>>> i.__doc__ = 'one' We just need help to print i.__doc__ instead of int.__doc__. |
This was done here: #84438. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: