3 ms·
print-as-a-function has a few nice qualities, like the ability to pass it around as an argument to things so you can do (stupid example): map(print, range(
by shabble 12y ago
print-as-a-function has a few nice qualities, like the ability to pass it around as an argument to things so you can do (stupid example):
map(print, range(10))
It can also take some kwargs that allow you to do things that are a bit clunky with the print statement.
Which is the more intuitive of the following?:
print(errmsg, file=sys.stderr)
print >>sys.stderr, errmsg
(I didn't even know about the latter until I found it in someone's code and looked it up) Also, suppressing line endings:
print "cats",
or
print("cats", end='')
My lazy-brain prefers the statement. My sensible/code-review brain prefers the function.
- tomp 12y agoThe first really has nothing to do with `print` being a statement - it could be parsed and passed as a function in "non-statement" positions regardless. The rest could probably be special-cased in a backwards-compatible way as well. This is currently not valid Python 2.0 syntax: print "cats", end='', file=sys.stderr
- shabble 12y agowell, as a statement it's also a reserved keyword, so you can't override or mock it (AFAIK), and I suspect changing the parser to identify context and operate accordingly might be rather painful. Honestly, I'm reasonably happy with the split; there don't seem to be many compelling reasons to shoehorn all the extra bits back into statement-print other than a) removing a single pair of parens b) being backwards compatible (but then the old code wouldn't be using those new features anyway, and would still have to support that nasty bitshift hack.