-
-
Notifications
You must be signed in to change notification settings - Fork 31.8k
The documentation for the print() builtin should perhaps say file=None as default #94286
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
I recently started working towards becoming python core developer. I came across this issue marked as easy. Can I take up and fix this issue? |
@ddurgoji This issue already has a corresponding PR, refer to the above |
iritkatriel
pushed a commit
that referenced
this issue
Nov 6, 2022
This was referenced Nov 6, 2022
miss-islington
pushed a commit
to miss-islington/cpython
that referenced
this issue
Nov 6, 2022
(cherry picked from commit 2db55e0) Co-authored-by: Nouran Ali <nouranalimohammed@gmail.com>
miss-islington
pushed a commit
to miss-islington/cpython
that referenced
this issue
Nov 6, 2022
(cherry picked from commit 2db55e0) Co-authored-by: Nouran Ali <nouranalimohammed@gmail.com>
miss-islington
added a commit
that referenced
this issue
Nov 6, 2022
miss-islington
added a commit
that referenced
this issue
Nov 6, 2022
24 tasks
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Documentation
The current function definition for the
print()
builtin at https://docs.python.org/3/library/functions.html#print reads:print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)
However, this suggests that the default value of
file
is the original version ofsys.stdout
at Python iniitialise time, not the current value ofsys.stdout
at the time the function is called. I just stumbled across a package which does this (function body stripped down to the bare minimum to highlight the issue):This looks fine, but it conceals a subtle issue, which is revealed when the function is called like this:
Since
contextlib.redirect_stdout
redefinessys.stdout
, but inmyfunc
, the default value ofstream
is the value ofsys.stdout
at function definition time, so this snippet still sends the output "message" tosys.stdout
rather thansys.stderr
.However,
print()
does not do this: print looks up the current value ofsys.stdout
every time it is called withoutfile=...
specified. It instead behaves like the following corrected version ofmyfunc
:My suggestion, therefore, would be to modify the definition given to read:
print(*objects, sep=' ', end='\n', file=None, flush=False)
Most readers will be unaffected by the change, especially as the behaviour of the function when
file=None
is specified is explicitly described in the second paragraph. But it hints that the best way to specify a default ofsys.stdout
in a function is to have the default beingNone
and to assignsys.stdout
in the body of the function.[Edit: fix name of
contextlib
function.]The text was updated successfully, but these errors were encountered: