print v. print()

F

Frederick Williams

I am learning Python from Hammond & Robinson's _Python Programming on
Win32_, January 2000 edition. This

print "Sleeping for 10 seconds"

which appears in some example code, fails to... um... Compile?
Interpret? Well, whatever the word is, it fails. Trial and error
revealed that

print("Sleeping for 10 seconds")

does the trick. I am using version 3.1.2, presumably the book's authors
used some earlier version.

So why the change from print to print()?

I should warn you that I know nothing about computers in general or
Python in particular.
 
T

Thomas Jollans

I am learning Python from Hammond & Robinson's _Python Programming on
Win32_, January 2000 edition. This

print "Sleeping for 10 seconds"

which appears in some example code, fails to... um... Compile?
Interpret? Well, whatever the word is, it fails. Trial and error
revealed that

print("Sleeping for 10 seconds")

does the trick. I am using version 3.1.2, presumably the book's authors
used some earlier version.

Yes, indeed. Python 3.0 changed a number of things, the most visible is
removing the print statement in favour of the print() function.
So why the change from print to print()?

There's no reason for print to be a statement -- it can just as well be a
function, which makes the language more regular, and therefore quite possibly
easier to learn.
 
M

Mel

Thomas said:
There's no reason for print to be a statement -- it can just as well be a
function, which makes the language more regular, and therefore quite
possibly easier to learn.

The downside to a print() function is that assigning to
`print` can mask the function, and leave a neophyte without
any way to get output out of the program.

The problem with that downside, I guess, is that rogue
assignment to sys.stdout can kill a print statement just as
dead as a print() function, so the statement's so-called
advantage is not that great.

Mel.
 
S

Steven D'Aprano

The downside to a print() function is that assigning to `print` can mask
the function, and leave a neophyte without any way to get output out of
the program.

On the other hand, the upside to a print() function is that assigning to
`print` can monkey-patch the function, allowing the advanced user to
modify it's functionality at runtime. Whether that's a feature or a
shooting offence is a matter of opinion.

(I think it's a feature, albeit one which is easy to misuse.)
 

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,170
Messages
2,570,927
Members
47,469
Latest member
benny001

Latest Threads

Top