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
The documentation for the print() builtin should perhaps say file=None as default #94286
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


juliangilbey commentedJun 26, 2022
•
edited by bedevere-bot
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
fileis the original version ofsys.stdoutat Python iniitialise time, not the current value ofsys.stdoutat 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_stdoutredefinessys.stdout, but inmyfunc, the default value ofstreamis the value ofsys.stdoutat function definition time, so this snippet still sends the output "message" tosys.stdoutrather thansys.stderr.However,
print()does not do this: print looks up the current value ofsys.stdoutevery 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=Noneis specified is explicitly described in the second paragraph. But it hints that the best way to specify a default ofsys.stdoutin a function is to have the default beingNoneand to assignsys.stdoutin the body of the function.[Edit: fix name of
contextlibfunction.]The text was updated successfully, but these errors were encountered: