[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Old Man Yells At Cloud

On 17/09/2017 15:42, Steve D'Aprano wrote:
> On Sun, 17 Sep 2017 11:51 pm, Tim Golden wrote:
>> Print-as-a-function removed one small simplicity
> Presumably you've never wanted to print to something other than std.out.

Actually, no. (stderror is either a Unix-ism or C-ism, or some combination).

> syntax in Python 2 is horrid:
> print >>sys.stderr, args

But I have wanted to print to a file. I wasn't aware of Py2 syntax for 
it, but that's not so different to what I normally use which is "@" in 
place of "<<". And the @, which used to be #, I think came from '#' in 
Basic or somewhere.

I understand from your example below that in Py3, the file is specified 
somewhere in a list of keyword parameters.

But since the print destination is quite important, it benefits from 
being stated up-front rather than buried somewhere near the end of all 
the main print operands.

> Presumably you've never wanted to print using a separator other than space. You
> simply can't do it at all in Python 2.
> Presumably you've never wanted to print and suppress the newline at the end of
> the line.

With the languages I normally use, newline isn't automatically written 
in the first place! (I'd write either print or println.)

However controlling the separator between items has been and still is a 
nuisance (if not using a formatting string, but this is about being able 
to write quick, throwaway prints).

Py3's 'sep' parameter is usable, mainly by requesting no separator and 
adding them manually as required. So if (a,b,c,d,e) have the values 
("(",10,20,30,")") and you wanted output of '(10 20 30)' I guess you 
would write:

  print (a,b," ",c," ",d,e, sep="")

(The scheme I currently use is to always add a space unless suppressed 
with ',,', so the same requirement is written as:

  println a,,b,c,d,,e

But they're /both/ ugly!)

> 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

And again, no. It has happened, and this was not in Python, that I 
wanted to print to a string (appending to one), or to the current 
location in a window or image. Then, you need a clear way specifying 
such a destination.

> Presumably you've never wanted to control when the output buffer is flushed.
> That's another thing you can't do in Python 2.

Once more, no. I suppose it would be necessary to call a separate 
function, which is handier in not cluttering up the main print.

> 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.

Sorry, no.

> 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.
> print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)

OK, in that case why not have both? Of course they would need to be 
given different names to make things easier.

Then there wouldn't be any argument.

> No more cryptic and ugly syntax for printing to a file. Setting the argument
> separator is easy. Instead of an obscure and easy to miss trick to suppress the
> end of line, you use an obvious and self-explanatory keyword argument.

   movedata(a, b, action='copy', depth='shallow')

Self-explanatory keyword arguments, tick; as simple as just writing:

   a = b

probably not!