Monday, February 23, 2009

Print command now as a function in Python 3.0

Recently I managed to play around with Python again. The Python 3.0 (now I'm using 3.0.1) treats the print as an ordinary function.

Last time it is a special command:

[sourcecode lang="python"]
print 'hello, world!'

which is treated as a different animal from any other Python functions. Now the Python 3.0 team has return print back to its mainstream:

[sourcecode lang="python"]
print('hello, world')

The behavior of weird comma, a GW-BASIC-like print command also has been fixed here, we have to append a comma to tell Python interpreter not to print a new line character after a print command:

[sourcecode lang="python"]
print "I don't want to skip to",
print "next line"

which will print (it adds space between the words "to" and "next line"):

I don't want to skip to next line

Sometimes, or most of the time, this behavior is considered weird. Of course it is not weird for a used-to-be-a-GW-BASIC/BASICA programmer like me. But for the rest of the world who only knows Pascal, C, Java, C++, etc it is a weird syntax!

Now Python 3.0 comes with the print as a normal Python function:

[sourcecode lang="python"]
print("I don't want to skip to", end="")
print("next line")

which will print:

[sourcecode lang="shell"]
I don't want to skip tonext line

It's good to see that now some of the inconsistencies have been fixed. Of course it took a lot of consideration as they don't want to break the compatibility with previous versions. The 3.0 release is of course an exception. It was released as a breaking version, there will be some backward incompatibility with previous versions (2.x).

No comments:

Post a Comment