Old Man Yells At Cloud
Tim Chase
python.list at tim.thechases.com
Sun Sep 17 14:09:23 EDT 2017
On 2017-09-18 00:42, Steve D'Aprano wrote:
> On Sun, 17 Sep 2017 11:51 pm, Tim Golden wrote:
> Presumably you've never wanted to print to something other than
> std.out. The syntax in Python 2 is horrid:
>
> print >>sys.stderr, args
For those cases, the old syntax was sufficiently horrid that indeed I
didn't use it, but rather used
print "Normal output"
sys.stderr.write("error output!\n")
Yes, adding the \n manually is a minor annoyance, but it wasn't much
of an issue.
> Presumably you've never wanted to print using a separator other
> than space. You simply can't do it at all in Python 2.
Would just assemble the string I wanted to print and then print that:
print "|".join(my_list)
print ", ".join(["hello", "world", "from", "william", "shatner"])
> Presumably you've never wanted to print and suppress the newline at
> the end of the line. The Python 2 syntax is obscure, error-prone,
> and easy to miss:
>
> print a, b, c, d, # WOT?
Very rarely wanted to do this, but in those rare cases, I'd usually
use sys.stdout.write()
> Presumably you've never wanted to pass print to something as a
> function. It can't be done in Python 2:
>
> something.register(callback=print) # SyntaxError
In these cases, I almost never wanted raw print() to be passed, but
something that did argument formatting:
def p(*args, **kwargs):
print fmt_string % args
return SUCCESS
⋮
something.register(callback=p)
> Presumably you've never wanted to control when the output buffer is
> flushed. That's another thing you can't do in Python 2.
Can use sys.stdout.flush() just fine in Python 2. I've never really
had a need/want to have it buffer when it would otherwise auto-flush
(such as writing to pipes).
> Presumably you've never wanted to monkey-patch an existing function
> that used print by shadowing the built-in. You can't do that in
> Python 2, since print isn't a regular name, it can't be mocked or
> shadowed or replaced.
I've never really had the want in production. Though again, I've
reassigned sys.stdout on a couple occasions
>>> import sys
>>> f = file('delme.txt', 'w')
>>> sys.stdout = f
>>> print "Hello"
>>> f.close()
> I've wanted to do all those things, and more. I love the new print
> function. For the cost of one extra character, the closing bracket,
> it gives you much, much more than the old print statement ever did.
That said, I'm neither here nor there when it comes to using
print-as-a-statement vs print-as-a-function. I like the consistency
it brings to the language, but miss the simplicity that Py2 had for
new users. I'd almost want to get it back as a feature of the REPL,
even if it wasn't part of the language itself, akin to how pdb can
override certain things in certain contexts:
(Pdb) l
1 j = 42
2 x = 31
3 import pdb; pdb.set_trace()
4 -> j = 15
5 x = 99
[EOF]
(Pdb) x
31
(Pdb) j 1
> /home/tkc/runme.py(1)<module>()
-> j = 42
(Pdb) j
*** The 'jump' command requires a line number
So, while I'm +0 on the move to print-as-a-function in actual code, I
find it most annoying in a pdb/REPL context where I would prefer to
still have the old print-as-a-statement for simplicity.
-tkc
More information about the Python-list
mailing list