What's new in Python 3 (aka Python 3000)

Python 3 is the new version of Python, and its known that it breaks backwards compatibility with Python 2.x. This knol should help you to convert your 2.x programs for Python 3.


Introduction


Python 3 was released on December 2008[1][2]. It is not expected that everybody will migrate right away because most third party dependencies won't be ready on the release date. If you have code to migrate to Python 3 and it depends on external libraries, wait until at least version 3.1 to start the migration. Python 3 is a viable option when you are starting a project from scratch and you can rely on built-in function and modules.  Although Python 2.x still has a long life ahead, it won't hurt to know what we can expect from the last big release of our favorite programming language.

⊗


Datatypes


  • All strings are now Unicode.
  • Remove distinction between int and long types (L suffix is gone in longs).
  • True division is standard (that is, 5/2 now gives 2.5 and not 2 as before)
  • dict.has_key(x) is invalid, use "x in dict"
  • dict.items(), dict.keys() and dict.values() now returns an iterator instead of a list (use list() to get a list).
  • A set can be declared with {1,2,3}  instead of set([1,2,3])

Functions


  • There are function anotations: def fn(x: int) -> str: (see [3] for more information)
  • apply(f,args,kw) is now f(*args, **kw)
  • print is now a function[4]: print(1,2,3, sep="",end="\n",file=sys.stdout), allowing it to be used inline rather than having a special syntax

Exceptions


raise Exception, "a message" is now: exception("a message")

Removed


  • xrange() is replaced with a new range() function, the new range() will return an iterator instead of a list.
  • raw_input() is replaced with a new input() function that does what the old raw_input() did.To use the old input(), do eval(input()) (from a security point of view this is not a good idea).

Compatibility with previous versions


Python 3.0 breaks backwards compatibility with Python 2.x. There is no requirement that Python 2.6 code will run unmodified on Python 3.0.[5] Here is the proposed path to migrate your 2.x programs to 3.0 by using Python 2.6:


  1. Port your project to Python 2.6.
  2. Turn on the Py3k warnings mode (with "-3" at command line).
  3. Test and edit until no warnings remain.
  4. Use the 2to3 tool to convert this source code to 3.0 syntax. Do not manually edit the output!
  5. Test the converted source code under 3.0.
  6. If problems are found, make corrections to the 2.6 version of the source code and go back to step 3.
  7. When it's time to release, release separate 2.6 and 3.0 tarballs (or whatever archive form you use for releases).

Python 3 talk by Guido Van Rossum



Python 3000


Comments

Good

It was short but sweet, as for accusiations of replication from python.org's page, there was no text directly lifted from python.org so it's not plagerism.

Last edited Jul 24, 2009 3:33 PM
Report abusive comment

Self.Pythong

Good article, exclaims to the python community the new built_in functions within' Python 3000. As usual I will stick to python 2.4 though.

Jan 31, 2009 1:38 PM
Report abusive comment

Outdated

I just discovered knol and the first article I start reading is outdated... I go back to wikipedia and wikibook ( http://en.wikibooks.org/ ) !

Last edited Jan 31, 2009 1:26 PM
Report abusive comment
Sebastian Bassi
Sebastian Bassi
Bioinformatics at Genes Digitales
Bernal

Contributors

Article rating:
Your rating:

Activity for this knol

This week:

20pageviews

Totals:

2886pageviews
11comments