Saturday, December 6, 2008

Python 3.0 (a.k.a Pyhon 3000, a.k.a. Py3K) is the new release of the most wonderful (in my biased opinion) language in the coding world.
The fact is that Python 3.0 happens to be incompatible with the 2.x releases. The language of course is the same but there are significant differences that make most of the scripts written say with 2.5 stop running if passed to a python 3.0 interpreter.
On December 4th Guido Van Rossum, the creator of Python, drew up a What's new in Python 3.0 document to be found here.
Since I've no intention to go through every detail of the changelog I'll limit myself to tell just the most relevant change (or those that seems that to me).
- print statement becomes a function (and that's one of the biggest pain in the ass for code written with previous releases).
before we used to write:
>> print "Wiz and Chips rocks!"now we must call the function:
>> print ("Wiz and Chips rocks!")there are however many more and less trivial effects on the print statement (ehm.. function) to impact the new coding style.
- The ordering comparison operators (<, <=, >=, >) raise a TypeError exception when the operands can't be naturally compared one another. Expressions like 1 < "", 0 > None or len <= len are no longer valid. This however doesn't apply to == and != operators.
long int type disappears. There's just one integer type int which essentially behave like the old long.
- An expression like 75/27 now returns a float. To get truncated value just use 75//27.
- The repr() of a long integer doesn't inlude the L character at the end of the representation. So if you have code that cut off that character by default it will now cut the last digit instead.
- Unicode strings and 8-bits string system is suppressed and changed by Python 3.0 with the concepts of text and binary data. The type used to store text is str and the type used to store data is bytes. Any attempts to mix text and data in Python 3.0 will raise a TypeError. This is a useful rationalisation as the previous system was inelegant and prone to generate flocks of bugs. This new concept however will certainly create lots of incompatibilities with codes out there which include both encoded and unencoded text. As str and bytes types can't be mixed they must explicitly converted (str.encode() --> str to bytes - bytes.decode() --> bytes to str). Bytes is immutable but there's a mutable form called bytearray.
as and with are reserved words in Python 3.0 (but they were introduced in 2.6)
- List comprehensions no longer support the syntactic form  for x in item1, item2, ..., we must use instead for x in (item1, item2, ...).
- The from MODULE import * syntax is only allowed at the module level, no longer inside functions.
- There's a new system for the string formating operations that replaces % operator. Thank God % it's still supported in 3.0 but will be deprecated by 3.1 and removed some time later. If you're interested in the new formatting system here's an extensive documentation about it.
raw_input() is replaced by input() which also means that 3.0 input() isn't the same as 2.x input()
These are the main points that caught my superficial attention. I don't plan switching to 3.0 in the immediate future. My bet is that Python 3.0 usage path will be that of a flat curve. My concern in this field is about modules and extensions. Python has simply tons of them which enable you to pursue plainly every kind of activity you have in mind. Despite the release builtin modules I can't conceive an app written in python which doesn't rely on third part modules. Well, let alone the enterprise developed and supported libraries (i.e. reportlab which currently doesn't support 3.0 anyway), the scenario of small and often very useful libraries to be converted to Python 3.0 is not so happy in my mind. It will take time and maybe some crucial library will maybe remain relegated to the 2.x version of the language for God knows how many months / years.
The good news with this respect is that a 2to3 source-to-source translator is provided to be run on your source code. This tool should do most of the works to translate your code to be Python 3.0 compliant. The bugs remaining after a run under 3.0 must be however be fixed manually. For more information regarding this tool please read this page.
A last concern regards performance. They say that Python 3.0 runs the pystone benchmark around 10% slower than Python 2.5.  They say there's room for improvement, however this is not so encouraging given that python's not known to be a fast language.
Python 3.0 is clearly a milestone release which main aim's that of rationalise and clean the language builtins. We will see if this release will improve the already boosting success of this wonderful language.

No comments:

Post a Comment