Take the 2-minute tour ×

Please help me understand what important features I've missed in Python since my last experience on 2.4 version.

Now I should choose a framework/language for my upcoming project, which will be time/memory critical and cross-platform, I'm choosing between Java and Python, and before that I want to make sure that I'm not judging Python 2.4.

Any help is appreciated.

share|improve this question
add comment

closed as too broad by MichaelT, gnat, Bart van Ingen Schenau, Martijn Pieters, GlenH7 Feb 23 at 0:40


3 Answers

up vote 8 down vote accepted

You could look at the "what's new in Python x.y" documents released since 2.4. Some changes I really appreciate and use regularily:

  • try/except/finally - seems like this was half-assed prior to 2.5 (in that except and finally were mutually exclusive)
  • with statement for deterministic cleanup. Basically, it outsources any cleanup (like closing files or other resources) in finally to the object in use. Example:
    with open('path', 'mode') as f:
        # use f
    # at this point, the file is closed, no matter what happens
    #(well, except interpreter crash ;))
  • new string formatting
  • In Python 3, many functions now return iterators instead of lists (builtin .+ replaced with itertools.i\1, dict.iterkeys() and friends replace dict.keys() and friends, xrange replaces range, etc). This can greatly reduce memory usage and is generally very neat :)
  • / always returns a float. Use // for integer/truncating division
  • Python 3 always uses unicode by default; there is bytes for when you actually want to work with bytes or a single-byte encoding.
  • Also, a whole lot of shiny, very useful libraries (both stdlib and third party)
share|improve this answer
Thank you, a very informative list. –  duros Dec 26 '10 at 19:13
add comment

Ordered Dictionaries

in Python 3.1

Regular Python dictionaries iterate over key/value pairs in arbitrary order. Over the years, a number of authors have written alternative implementations that remember the order that the keys were originally inserted. Based on the experiences from those implementations, a new collections.OrderedDict class has been introduced.

The OrderedDict API is substantially the same as regular dictionaries but will iterate over keys and values in a guaranteed order depending on when a key was first inserted. If a new entry overwrites an existing entry, the original insertion position is left unchanged. Deleting an entry and reinserting it will move it to the end.

share|improve this answer
collections.OrderedDict is also in 2.7. –  sdolan Dec 26 '10 at 10:49
add comment

One of the big annoyances when I tried Python 3.0 was that:

print "Hello world!"

had been changed to:

print("Hello world!")


share|improve this answer
Seriously. That irritates me. –  ykombinator Dec 25 '10 at 16:27
It's better this way (more regular, less obscure syntax for printing to files/stderr/..., can optionally specify end and separator, etc). The only problem is that it breaks backwards-compability (and habit). –  delnan Dec 25 '10 at 18:10
@delnan - I said it was an annoyance, not wrong or a bad direction to head towards. Making print a function has its uses but it's a bit of a surprise when you come from Python 2 :) –  andre Dec 25 '10 at 18:11
Yes, but I've seen one to many people bitching about this, so I though I better write the upsides down as soon as possible :) –  delnan Dec 25 '10 at 18:14
Yeah, the first python I learned was 3.x - but all of the sample code I could find on the web was 2.x code, so I had to update all the print statements to make it work... Even the python documentation is written for 2.x, or was. Maybe they've added 3.x tutorials now. –  Margaret Dec 25 '10 at 20:52
add comment
