-
-
Notifications
You must be signed in to change notification settings - Fork 29.8k
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
return value of builtins is not clearly indicated #90440
Comments
Do the print docs need to mention something so obvious? Functions and methods which operate by side-effect typically don't mention that they return None, see for example the docs for various builtin methods: https://docs.python.org/3/library/stdtypes.html#mutable-sequence-types e.g. s.append, s.clear, s.extend, s.insert, s.remove, s.reverse and likewise for list.sort, set.add, dict.clear and many others. (However, dict.update is a rare exception, it does mention that it returns None.) We should not feel it necessary to add an explicit comment to every function or method that operates by side-effect stating that they return None. |
I opened this ticket on behalf of a user who asked about print() specifically in #python on the Libera IRC network, who I assume does not find this obvious. I don't think it would be tenable to add this note to every built-in, but that's not the intended scope of this issue. I do think it's worth mentioning for print(), though. |
I think it does a better service to users to explain how Python returns None implicitly if a function doesn't have any other explicit return value. If the print() docs had this note, it would be confusing why other similar functions don't. It's also worth explaining that when a function is designed to explicitly return None in certain cases (e.g. dict.get()) that it shouldn't do so implicitly, but should include an explicit |
Sure, there will always be some users who will find certain things not What did this user think print() returned? What precisely was their I can see a difference between (for example) the questions: "I see that print('Hello world') returns None, but is it safe to assume and "What does x = print('Hello world') do?" |
The original question was closer to the related issue of "indicate return types for all built-ins," conversation log follows (UTC-5):
|
How about following "The Python interpreter has a number of functions and types built into it that are always available. They are listed here in alphabetical order." in Barry: The PEP-8 'return None' recommendation could be added to the Reference entry for 'return'. But I think this should be a separate issue as 1) it is about coding rather than documentation and 2) there is the possible objection that choosing completely explicit 'return None' versus half explicit, half implicit 'return' and the latter versus completely implicit <nothing at end> should be left to the style PEP. |
I do think it's a question of style. Section 7.6 of the language reference says:
which is the important concept that beginners should learn. I agree that the admonition in PEP-8 is really trying to say "don't mix implicit and explicit return styles". Implicit None return is fine if all exit paths are implicit. But once you add an explicit value to a return path, all return paths should use explicit values, including those that return None. IME, while I do occasionally encounter push back on this when doing reviews, most folks come around to this p.o.v. |
The new title is puzzling; the discussion moved to advertising that all functions default to returning None, this is not related to the built-in status. |
This is already communicated in § 4.7 ("Defining Functions") of the official tutorial. I think it would be a good idea to revise that section so that this property of functions is a little more clear, but that isn't the scope of this ticket. The title change reflects my intent to submit a PR that adds a hint to the builtins doc. |
Please post your proposed change here to be discussed by participants here. |
My thought was to add something like this to the top of functions.rst:
For reference, the builtins this applies to are:
Which makes me wonder if the hint is even worth having, since it's so few of them. Note that of these, exec() does what this ticket originally proposed for print() - i.e. it explicitly says that the function returns None. |
This sounds to me like "by default they return None but you can override this default". I don't think any change to the doc makes sense here. When you state the obvious people wonder what they're missing. |
Steven, I am also inclined to close this. What do you think after the discussion? It is sometimes easier to clarify when we have a confused person present in the discussion. |
Obvious to who? New learners coming to Python don't know the same things as people with experience. |
IMO, new learners will be worse off by adding "returns None" to all of the builtins. At best it a distractor. I work with new learners almost every day. The issue never arises. No one writes "x = print('hello world')" and they would be worse off if shown such a possibility. One other consideration is that MyPy and the tools that use it (such as PyCharm) reject the idea of None as return value. Their view is that no value is returned all. We don't want the docs to contradict that world view. $ cat hello.py
x = print('hello world')
$ mypy hello.py
hello.py:1: error: "print" does not return a value
Found 1 error in 1 file (checked 1 source file) |
What we're debating here is a micro-cosm of the broader "documentation philosophy" questions that I'm hoping the Documentation WG can iron out. What is "obvious"? Is it obvious that print returns None when file.write does not? Why does "exec" explicitly say it returns None, and no beginners were harmed, but having "print" say it returns None would be bad? |
I should have said "redundant information" rather than "obvious". I consider it redundant to specify that the default behavior applies to some specific case. If I read redundant information I may pause to think why it was necessary to explicitly state it, and whether I am misunderstanding something. (Let's not make my bad choice of word turn this into a discussion about "what is obvious". The issue will probably never be closed if we do that.) |
Discussion about this side issue came up again on Libera, see #92753. |
Instead of specifying the return type in the text, which is a bit verbose and easy to miss in a wall of text, the return types could be mentioned in the function signature (if they are not too complicated). This is already done in some places and this one specifies the parameter type |
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: