Python 3.0: New and Backwards Incompatible!?

Fri, Dec 12, 2008 (Programming)

python-logoFor those looking to use Python 3.0 for all their development work, be warned. Changes in the latest version of Python were created to be intentionally not backwards compatible. According to the Python site “there are more changes than in a typical release, and more that are important for all Python users.” They also state that “Python really hasn’t changed all that much – by and large, we’re mostly fixing well-known annoyances and warts, and removing a lot of old cruft.” That being said what exactly has changed?

Well, for starters:

  • Print is now a function invoked as print() instead of print
  • Lists are no longer returned for well known methods (dict.keys(), dict.items(), dict.values()). Instead these methods return Views. map() and filter now return iterators instead as well.
  • Ordering comparison operators now raise a TypeError Exception when the operators don’t have a “meaningful natural ordering”
  • Long has been renamed to int so there is only one built-in integer type which behaves like the old long type.
  • Expressions like 1/2 will return a float while 1//2 will give you the truncated behavior
  • Binary data and Unicode use has changed. Python uses the concepts of text and binary data instead of Unicode strings and 8-bit strings. If your application is using these old concepts, you will be required to make modifications. Check the doc here

There have also been quite a few new syntax additions and some changes as well:

  • Binary and Octal literals are specified as 0b0110 and 0o720 respectively. (this was present in 2.6)
  • Keyword-only arguments have been added to function calls
  • New annotations for functions and return values potentially help 3rd party libraries (the example they give on the Python website allows libraries to use this to improve help messages
  • Nonlocal has been added for variables increasing scope but not making global.
  • Extended Iterable Unpacking

Of course, this is just a small list of the over all changes, there is still a list of syntax changes and, furthermore, a list removals. You can check the full list here and the actual changelog for the down-and-dirty facts. So do these changes have a negative impact? For now, the Python website lists 3.0 as being 10% slower over 2.5, and stated that this is possibily due to the removal of small casing for small integers. Of course this is still an x.0 release and there is plenty of room for improvements so don’t let that dissuade you.

One thing to note for code maintainers, it was suggested not to write code meant to be runnable on 2.6 and 3.0. Due to the wide list of changes supporting both versions would will result in a “very contorted coding style.” All of that being said, the new version comes with a source-to-source conversion utility called 2to3 to help with getting up to speed on the latest and greatest. Check out the new release, and happy coding!

Share This on Your Favorite Social Network:
  • Facebook
  • MySpace
  • Twitter
  • Digg
  • StumbleUpon
  • LinkedIn
  • Reddit
  • FriendFeed
  • Tumblr
  • Suggest to Techmeme via Twitter
  • Technorati
  • Mixx
  • Propeller
  • Fark
  • Slashdot
  • del.icio.us
  • Google Bookmarks
  • Yahoo! Buzz
  • Print
, , , ,

This post was written by:

Ray Gomez - who has written 46 posts on kallasoft.

Ray, a Linux and Unix nut, spends a majority of his daily ritual programming and testing for Big Blue. In his free time he manages to tweak the currently running thinkpad+KDE4 (WHOA) setup, read, and he occasionally gets out of the fluorescent lights to play roller hockey.

Contact the author

Leave a Reply