python 3's adoption

Steven D'Aprano steven at REMOVE.THIS.cybersource.com.au
Wed Jan 27 19:47:41 EST 2010


On Wed, 27 Jan 2010 18:29:25 +0100, Alf P. Steinbach wrote:

> The main problem with the incompatibility is for porting code, not for
> writing code from scratch.

Correct. It's a trivial problem, but still a problem.

> It's also a problem wrt. learning the language.

This makes no sense. Why is it harder to learn

print(x, y, z)

than this?

print x, y, z

The first case is like the other functions you have to learn, like len(). 
In fact, many newbies to Python put unneeded parentheses around arguments 
to print simply because they assume it is a function.

I would argue that the new print function is *simpler* to learn. It is 
more consistent with other built-ins, and has fewer magic idioms to 
learn. Instead of:

print >>fileObj, x, y, z

you use regular function syntax with a meaningful keyword:

print(x, y, z, file=fileObj)

If you want suppress the newline at the end of each print:

print x, y, z,  # note the final comma

compared to:

print(x, y, z, end='')

If you want to change the space between elements, instead of:

sys.stdout.write(str(x) + "*" + str(y) + "*" + str(z) + '\n')

you use:

print(x, y, z, sep='*')


If you want to override the behaviour of print in a module, instead of 
having to edit the source code of the module (which might not even be 
available), all you need to do is monkey-patch it:

import module
module.print = myprint





-- 
Steven



More information about the Python-list mailing list