py3k: adding "print" methods to file-like objects

This might be a minor thing, but I kind of wish that I could write this: sys.stderr.print('first line') sys.stderr.print('another line here') sys.stderr.print('and again') instead of: print('first line', file=sys.stderr) print('another line here', file=sys.stderr) print('and again', file=sys.stderr) As it's a lot easier to read for me. Of course you can always add spaces to make the lines line up, but with a long print statement your eye has to go a long distance to figure out what file, if any, you're printing to. It could be pretty simple to add: class ...: def print(*args, **kwargs): io.print(file=self, *args, **kwargs) I haven't been able to find any discussion on this, has this already been rejected?

I prefer using partial than introducing new syntax: print_to_stderr = functools.partial(print, file=sys.stderr) print_to_stderr('first line') print_to_stderr('second line') ... - Tal On Thu, Mar 13, 2008 at 10:56 PM, Erick Tryzelaar < idadesub@users.sourceforge.net> wrote:
This might be a minor thing, but I kind of wish that I could write this:
sys.stderr.print('first line') sys.stderr.print('another line here') sys.stderr.print('and again')
instead of:
print('first line', file=sys.stderr) print('another line here', file=sys.stderr) print('and again', file=sys.stderr)
As it's a lot easier to read for me. Of course you can always add spaces to make the lines line up, but with a long print statement your eye has to go a long distance to figure out what file, if any, you're printing to. It could be pretty simple to add:
class ...: def print(*args, **kwargs): io.print(file=self, *args, **kwargs)
I haven't been able to find any discussion on this, has this already been rejected? _______________________________________________ Python-ideas mailing list Python-ideas@python.org http://mail.python.org/mailman/listinfo/python-ideas

On Thu, Mar 13, 2008 at 3:56 PM, Erick Tryzelaar <idadesub@users.sourceforge.net> wrote:
This might be a minor thing, but I kind of wish that I could write this:
sys.stderr.print('first line') sys.stderr.print('another line here') sys.stderr.print('and again')
instead of:
print('first line', file=sys.stderr) print('another line here', file=sys.stderr) print('and again', file=sys.stderr)
As it's a lot easier to read for me. Of course you can always add spaces to make the lines line up, but with a long print statement your eye has to go a long distance to figure out what file, if any, you're printing to. It could be pretty simple to add:
class ...: def print(*args, **kwargs): io.print(file=self, *args, **kwargs)
I haven't been able to find any discussion on this, has this already been rejected?
It was brought up, considered, and rejected. The reason is that it would require *every* stream-like object to implement the print() functionality, which is rather hairy; or subclass a specific base class, which we traditionally haven't required. Making it function that takes a file argument avoids these problems. And, by the way, it's too late to bring up new py3k proposals. -- --Guido van Rossum (home page: http://www.python.org/~guido/)

On Thu, Mar 13, 2008 at 3:56 PM, Erick Tryzelaar <idadesub@users.sourceforge.net> wrote:
instead of:
print('first line', file=sys.stderr) print('another line here', file=sys.stderr) print('and again', file=sys.stderr)
Perhaps it would help if there were a function fprint(f, *args): print(file = f, *args) then the above could be written fprint(sys.stderr, 'first line') fprint(sys.stderr, 'another line here') fprint(sys.stderr, 'and again') which to me is a lot easier to read, since the file argument is in a consistent place, making it easier to see that it's the same from one line to the next. Also, it enables making the file argument very abbreviated, e.g. f = sys.stdout fprint(f, 'first line') fprint(f, 'another line here') fprint(f, 'and again') Otherwise, the shortest you can get it down to is 'file=f', which is 6 times as long. It might not seem much, but that's 5 less characters of print arguments that you can fit in without having to split the line. -- Greg

On Fri, Mar 14, 2008 at 5:27 PM, Greg Ewing <greg.ewing@canterbury.ac.nz> wrote:
Also, it enables making the file argument very abbreviated, e.g.
f = sys.stdout fprint(f, 'first line') fprint(f, 'another line here') fprint(f, 'and again')
Otherwise, the shortest you can get it down to is 'file=f', which is 6 times as long. It might not seem much, but that's 5 less characters of print arguments that you can fit in without having to split the line.
In that case I think partials a better option, when you could do: p = partial(print, file=sys.stderr) p('first line') p('another line here') p('and again') I completely forgot about partial which does a good job of filling in for what I wanted. I just need to consider the combination of the py3k stuff a bit more.
participants (4)
-
Erick Tryzelaar
-
Greg Ewing
-
Guido van Rossum
-
Tal Einat