tags:

views:

458

answers:

7

I've been teaching myself Python by working through Dive Into Python by Mark Pilgrim. I thoroughly recommend it, as do other Stack Overflow users.

However, the last update to Dive Into Python was five years ago. I look forward to reading the new Dive into Python 3 When I make the switch to 3.x, but for now, using django means I'll stick to 2.x.

I'm interested to know what new features of Python I'm missing out on, if I've used Dive Into Python as my primary resource for learning the language. A couple of examples that I've come across are

  • itertools
  • ElementTree

Is there anything else I'm missing out on?

edit: As Bastien points out in his answer, I could just read the What's New in Python pages, but sometimes it's fun to discover a useful tip on Stack Overflow rather than struggle through the complete, comprehensive answer in the official documentation.

A: 

I suggest that you read the “what's in Python 2.x?” documents. Some things that may have missed:

  • New-style classes (allows standard types subtyping, properties, ...).
  • The with keyword, which helps allocating and releasing resources.
Bastien Léonard
New classes were already in 2.2 (which Mark was writing about).
Alex Martelli
A: 
  • generator expressions
  • function decorators
  • metaclasses
Ed L
Metaclasses were already in 2.2 (which Mark was writing about).
Alex Martelli
+4  A: 

Mark(author of the book) had some comments on this. I've shamelessly copied the related paragraph here:
"""If you choose Python 2, I can only recommend "Dive Into Python" chapters 2-7, 13-15, and 17. The rest of the book is horribly out of date."""

sunqiang
That's good advice. I might have skipped through the XML and SOAP chapters quicker if I'd read that before.
Alasdair
+3  A: 

Here's a couple of examples of the sort of answer I was thinking of:

Conditional Expressions

Instead of the and-or trick, 2.5 offers a new way to write conditional expressions.

#and-or trick:
x = condition and 'true_value' or 'false_value'

#new in 2.5:
x = 'true_value' if condition else 'false_value'

Testing for keys in dictionaries

has_key() is deprecated in favor of key in d.

>>>d={'key':'value','key2':'value2'}
>>>'key1' in d
 True
Alasdair
This should probably be part of the question as examples of the type of answers that you're looking for. (But I'm slightly bitter because it has more up-votes than my answer. ;-)
cdleary
@cdleary I don't think there's anything wrong with answering your own question on SO. My answer really is an answer, because I discovered both examples in the What's New in Python documents after asking the question.I liked your answer, and up-voted it. I'm pleased that I'm familiar with most of the things you mention - it shows that learning with Dive Into Python hasn't left me writing archaic 2.2 code.
Alasdair
+5  A: 

Check out What's New in Python. It has all the versions in the 2.x series. Per Alex's comments, you'll want to look at all Python 2.x for x > 2.

Highlights for day-to-day coding:

Enumeration: Instead of doing:

for i in xrange(len(sequence)):
    val = sequence[i]
    pass

You can now more succinctly write:

for i, val in enumerate(iterable):
    pass

This is important because it works for non-getitemable iterables (you would otherwise have to use an incrementing index counter alongside value iteration).

Logging: a sane alternative to print-based debugging, standardized in a Log4j-style library module.

Booleans: True and False, added for clarity: return True clearer intention than return 1.

Generators: An expressive form of lazy evaluation

evens = (i for i in xrange(limit) if i % 2 == 0)

Extended slices: Builtins support strides in slices.

assert [1, 2, 3, 4][::2] == [1, 3]

Sets: For O(1) lookup semantics, you no longer have to do:

pseudo_set = {'foo': None, 'bar': None}
assert 'foo' in pseudo_set

You can now do:

set_ = set(['foo', 'bar'])
assert 'foo' in set_

Reverse iteration: reversed(sequence) is more readable than sequence[::-1].

Subprocess: Unifies all the ways you might want to invoke a subprocess -- capturing outputs, feeding input, blocking or non-blocking.

Conditional expressions: There's an issue with the idiom:

a and b or c

Namely, when b is falsy. b if a else c resolves that issue.

Context management: Resource acquisition/release simplified via the with statement.

with open(filename) as file:
    print file.read()
# File is closed outside the `with` block.

Better string formatting: Too much to describe -- see Python documentation under str.format().

cdleary
I should also note generators are more than an expressive form of lazy evaluation nowadays -- they support full coroutine behaviors. It's simpler to describe them the former way. :-)
cdleary
Oh, and simple generators let you write simple state machines very easily in day-to-day work, via `yield`. This is handy for things like custom iterators.
cdleary
+1  A: 

A few "minor" features were added in 2.4 and are pervasive in new 2.x python code: decorator (2.4) and try/except/finally clauses. Before you could not do:

try:
    do_something()
except FunkyException:
    handle_exception():
finally:
    clean_up()

Both are essentially syntactic sugar, though, since you could do the same, just with a bit more code.

David Cournapeau
+2  A: 
import antigravity

See the documentation

Lucas McCoy