what's the big deal for print()

P

pipehappy

Hi,

Why people want print() instead of print str? That's not a big deal
and the old choice is more natural. Anyone has some clue?
 
J

John Gordon

In said:
Why people want print() instead of print str? That's not a big deal
and the old choice is more natural. Anyone has some clue?

Because the new Python uses print(). print "str" is the old way.
 
T

Terry Reedy

Hi,

Why people want print() instead of print str? That's not a big deal
and the old choice is more natural. Anyone has some clue?

print as a function instead of a statement is consistent with input as a
function, can be overridden with custom versions, can be passed to
functions as an argument, and can have options passed as arguments
instead of with terrible syntax hacks.
 
S

steve+comp.lang.python

John said:
In <d3558c5d-0fe6-4da7-843d-c2f45b2bf869@y13g2000yqy.googlegroups.com>


Because the new Python uses print(). print "str" is the old way.


I think you missed the point of the question, which is, *why* does the new
Python (version 3+) use a function print() instead of a statement?

The problems with print as a statement includes:

It requires special treatment in the compiler, instead of just being an
ordinary function like len(), etc.

It's hard to come up with special syntax to add extra functionality to print
statement. Compare the ugly syntax needed to add support for printing to
writable files in Python 2:

print >>fileobj, arg # what does the mysterious >> syntax mean?

compared to the natural way it works in Python 3:

print(arg, file=fileobj)

Likewise, changing the delimiter between arguments. Python 3 has:
1*2*3

while Python 2 requires you to generate the string by hand, and then print
it:
1*2*3


One Frequently Asked Question is "How do I get rid of the newline after
printing?" In Python 2, you leave a comma at the end of the print
statement. What? A comma? How does that make *any* sense at all???
Unfortunately, while that gets rid of the newline, it also leaves spaces
between items:
.... print 1,
.... print 2,
.... print 3
....1 2 3

Here's the Python 3 version:
.... print(1, sep='', end='')
.... print(2, sep='', end='')
.... print(3, sep='')
....123


To get the same result in Python 2, you have to use sys.stdout.write().


The canonical explanation for why print is now a function is here:

http://www.python.org/dev/peps/pep-3105/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,160
Messages
2,570,889
Members
47,421
Latest member
StacyTaver

Latest Threads

Top