python 3's adoption

Alf P. Steinbach alfps at start.no
Wed Jan 27 16:47:04 CET 2010


* Daniel Fetchinson:
>>>>>>     * Print is now a function. Great, much improvement.
>>>> Actually not, IMHO. All it does is is to provide incompatibility.
>>>
>>> What incompatibility are you exactly talking about?
>>>
>>> Python 2.6.2 (r262:71600, Aug 21 2009, 12:23:57)
>>> [GCC 4.4.1 20090818 (Red Hat 4.4.1-6)] on linux2
>>> Type "help", "copyright", "credits" or "license" for more information.
>>>>>> print( 'hello' )
>>> hello
>>>>>> print 'hello'
>>> hello
>>>
>>> Yes, this is with python 2.6.2 which is in the 2.x line of releases. So?
>> I gather that your example is about code that technically executes fine with
>> both versions and produces the same result, i.e. that there is a subset with
>> the
>> same syntax and semantics.
>>
>> But 'print' calls that technically execute fine with both versions may and
>> will
>> in general produce different results.
>>
>> I.e. not just the syntax but also the semantics have changed:
>>
>>
>>  >>> import sys
>>  >>> sys.version
>> '2.6.4 (r264:75708, Oct 26 2009, 08:23:19) [MSC v.1500 32 bit (Intel)]'
>>  >>>
>>  >>> print( "2+2 =", 2+2 )
>> ('2+2 =', 4)
>>  >>> _
>>
>>
>>  >>> import sys
>>  >>> sys.version
>> '3.1.1 (r311:74483, Aug 17 2009, 17:02:12) [MSC v.1500 32 bit (Intel)]'
>>  >>>
>>  >>> print( "2+2 =", 2+2 )
>> 2+2 = 4
>>  >>> _
> 
> True. However, as someone else pointed out in a neighbouring thread you can do
> 
> Python 2.6.2 (r262:71600, Aug 21 2009, 12:23:57)
> [GCC 4.4.1 20090818 (Red Hat 4.4.1-6)] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
>>>> from __future__ import print_function
>>>> print( "2+2 =", 2+2 )
> 2+2 = 4
> 
> which gives 100% compatibility as far as print is concerned between 2.6 and 3.x.

That makes the code behave with 3.x syntax and semantics regarding print. I.e. 
it chooses one language. It doesn't make them compatible: if they were, then you 
wouldn't have to choose.

For example, it doesn't fix up 2.x code that prints tuples: incompatible, it 
will still execute but no longer do what it was supposed to do.

But lest you focus down on that rare case, consider 2.x code that contains

   print "2+2", 2

All such statements need to be fixed up in a 2->3 transition. Adding __future__ 
just makes the code invalid, it doesn't give you any shred of compatibility for 
this. A code rewrite can be partially automated like 2to3 but the 
incompatibility needlessly introduces an additional set of things that Can Go 
Wrong(TM), and with Murphy present, as He always is, well.


> So again, what sort of an incompatibility are you talking about
> concerning 'print' between 2.6 and 3.x?

Syntax and semantics. They're different.


Cheers & hth.,

- Alf



More information about the Python-list mailing list