python 3's adoption

Alf P. Steinbach alfps at start.no
Thu Jan 28 04:12:18 EST 2010


* Steven D'Aprano:
> 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

I think it's actually easier to learn just the 3.x form.

But it's more difficult to learn that there are /two different/ syntactic forms, 
depending on which version of Python you're using and in addition depending on 
__future__ declarations!

E.g., picking up some example code from the web, and it does not work...


> 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.

Yes, yes, yes, I agree.


> 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='')

Actually I thought the final comma thing was nice. It was like Basic. I think 
the 2.x 'print' must have been modeled on Basic's 'print'.


> 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

   >>> import builtins
   >>>
   >>> org_print = print
   >>> builtins.print = 666
   >>>
   >>> print( "trallala" )
   Traceback (most recent call last):
     File "<stdin>", line 1, in <module>
   TypeError: 'int' object is not callable
   >>> org_print( "but is that really so smart?" )
   but is that really so smart?
   >>> _


Cheers,

- Alf



More information about the Python-list mailing list