Alasdair
Alasdair

Reputation: 308889

Changes to Python since Dive into Python

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

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.

Upvotes: 12

Views: 1517

Answers (6)

Alasdair
Alasdair

Reputation: 308889

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

Upvotes: 3

Kredns
Kredns

Reputation: 37211

import antigravity

See the documentation

Upvotes: 1

David Cournapeau
David Cournapeau

Reputation: 80710

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.

Upvotes: 2

cdleary
cdleary

Reputation: 71424

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().

Upvotes: 9

sunqiang
sunqiang

Reputation: 6492

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."""

Upvotes: 6

Bastien Léonard
Bastien Léonard

Reputation: 61713

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.

Upvotes: 0

Related Questions