Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changes to Python since Dive into Python

Tags:

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

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

like image 968
Alasdair Avatar asked Jul 03 '09 20:07

Alasdair


2 Answers

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

like image 58
cdleary Avatar answered Sep 30 '22 05:09

cdleary


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

like image 38
sunqiang Avatar answered Sep 30 '22 05:09

sunqiang