Mastering Python... Best Resources?

Roy Smith roy at panix.com
Fri Aug 26 11:12:32 EDT 2011


In article 
<2309ec4b-e9a3-4330-9983-1c621ac16163 at ea4g2000vbb.googlegroups.com>,
 Travis Parks <jehugaleahsa at gmail.com> wrote:

> I know the Python syntax pretty well. I know a lot of the libraries
> and tools. When I see professional Python programmer's code, I am
> often blown away with the code. I realized that even though I know the
> language, I know nothing about using it effectively.

In a sense, I'm in the same boat as you.  I've been using Python since 
before the 2.0 series, and I tend to think of the language in much the 
same way as I did back then.  Which is to say I don't use the language, 
as it currently exists, as effectively as I might.

Here's some things I suggest you look at:

Iterators.  This is such a powerful concept.  When I started with the 
language, iterators largely meant the difference between range() and 
xrange().  Now we've got a whole ecosystem which has grown up around 
them (x + " comprehension" for x in {'list', 'dictionary', 'set'}), not 
to mention generators and generator expressions.  And the itertools 
library.

Decorators.  Another powerful concept.  We use these in our web servers 
for all sorts of cool things.  Adding cacheing.  Imposing prerequisites 
on route calls.  I still don't think of using these immediately, but I 
do see the notational convenience they provide for many things.

Context Managers.  One of the (very few) things that I always found 
lacking in Python compared to C++ was deterministic object destruction.  
Context managers give you this.  I'm still exploring all the neat things 
you can do with them.

The full range of containers.  I started with lists, tuples, and 
dictionaries.  Now we've got sets, frozensets, named tuples, deques, 
Counters, defaultdicts (I love those), heaps, and I'm sure a few others 
I've missed.  List and dicts are such well designed containers, you can 
do almost anything with just those two, but all the other new ones often 
make things quicker, simpler, and more obvious.

The profiler.  Most people obsess about performance early on and don't 
realize that most of their guesses about what's fast and what's slow are 
probably wrong.  Learn to use the profiler and understand what it's 
telling you.

Unittest.  Testing is, in general, a neglected practice in most software 
development shops, and that's a shame.  Python has some really good 
capabilities to support testing which you should get familiar with.  
Unittest is just one of them.  There's also doctest, nose, and a bunch 
of other contributed modules.  Look at them all, learn at least one of 
them well, and use it for everything you write.

> I've read quite a few books about Python. They cover a lot of topics,
> but none of them covered common conventions or hacks. I mean, I got
> good at C++ reading books by Scott Meyers, who concentrated on common
> idioms, things to avoid, the proper way to do things, etc.

Ugh.  The problem with Meyers's books is that they are needed in the 
first place.  C++ is such a horribly complicated language, you really 
can't use it without making a serious study of it.  There's too many 
gotchas that you MUST know to avoid disaster with even the most basic 
programs.

Python isn't that way.  You can learn a small, basic subset of the 
language and get a lot done.  You may not be doing things the most 
effective way, but you're also not going to be looking at memory 
corruption because you didn't understand the details of object lifetimes 
or how type promotion, function overloading, and implicit temporary 
object construction all interact.



More information about the Python-list mailing list