print with no newline

Alex Martelli aleaxit at yahoo.com
Sat Sep 4 05:00:27 EDT 2004


Tim Golden <tim.golden at viacom-outdoor.co.uk> wrote:
   ...
> I would *not* reasonably expect print to be a function. Why
> should it be?

Because the concept of print being a statement it has no true advantage,
and several disadvantages, compared with the obvious alternative of it
being a built-in function.  'print' becomes a keyword, which is always a
cost -- try accessing a member named 'print' in any kind of ffi or
distributed-programming interface (be it COM, Corba, xml/rpc, whatever)
and you'll see why minimizing reserved words is a great thing... try
making a dict with the string 'print' as a key and suddely you can't use
the cool syntax dict(akey=23) and have to quote... you can't use print
in a lambda nor any other expression, you can't pass print to a higher
order function requiring a callable, you can't store it in a list of
dict -- all wonderful things you can do with functions but not with
statements.

Hoping that you can see there ARE disadvantages in having print be a
statement, let's look for advantages.  The ability to not use
parentheses isn't really one -- parentheses are nice, they let you just
break long lines as needed for example.  If lack of parentheses WERE
important, Python would support "foo bar" meaning foo(bar), but it
doesn't.  What else?  The mysterious end-with-a-comma syntax to remove
the default newline?  Teaching that to beginners is a chore, while a
function would have a keyword argument such as endline=True, and passing
endline=False would suppress the end-line.  Instead of the horrid >>file
syntax we'd have file=sys.stdin.  And if the BDFL ever decides some
other optional nondefault behavior is warranted, having keyword args
would make expressing it a snap, while, with print as a statement, it's
a huge problem.

I hope you see now why many of us hard-boiled Pythonistas dislike print
being a statement and consider it a wart in Python.


> Certainly any move to remove the print statement
> from the language or to replace it by a print function would
> get my thumbs-down (or whichever way round the Romans did it).

We'll fight that batte at Python 3.0 time, then.  And I'm giving some of
my side's ammunition away, tch...!-)  What heartens me is that it's not
only the usual suspects -- experts who typically teach Python to other
experienced programmers, not to total newbies -- who agree with me on
this; I have an ally on this specific subject who's among the paladins
of NON-programmers... my wife, Anna (her background is primarily in
training -- she started programming relatively recently, and with Python
as her first language discounting the modest Basic and Pascal exposure
she got back in high school).

While the BDFL is typically not really swayed by argument -- what does
help him make up his mind is one of life's mysteries... -- he doesn't
discount argument 100%, so you guys liking print as a statement should
eventually start preparing your own arguments (no hurry, Python 3.0
won't happen for years yet).  They don't have to be quite as powerful as
those for print being a builtin function (they can't -- print being a
builtin function is RIGHT, it being a statement is WRONG, so obviously
the arguments for the former stance are inherently more powerful than
those for the latter!-) but they need to be decent enough to help Guido
rationalize what his nose suggests ot him, should that suggestion lean
towards keeping print a statement...!-)


Alex



More information about the Python-list mailing list