Tutorial for Python "way of programming"?

Peter Hansen peter at engcorp.com
Thu Dec 19 22:29:13 EST 2002


Colin Cashman wrote:
> 
> > Are there any guides, or tutorials, that would give me a heads-up on
> > these subjects?
> 
> Answering my own question, it appears PEP-8 is a great starting place.

It is, but you also need to learn the various Python idioms for things.
A simple example: newcomers (well, from C, if not from C++) might try
writing for-loops which iterate an index variable through some range
of values, and use it to index into a list to access each element.
In Python, of course, lists are directly iterable with the for statement,
so you just say "for someitem in somesequence: print sometime" and
get the same effect, without the index variable.

Another case: you almost never want to inspect and manipulate strings
one character at a time in Python.  There are standard modules that
do a much better job, and string methods, and things like slices.

Although there may be a page somewhere on Python idioms, you can 
probably learn an awful lot from the Python Cookbook on the ActiveState
site.  Better yet, buy the book, which is based on the online cookbook.
It has *excellent* introductory "treatises" for each chapter -- words
carefully chiseled in stone after being channeled from the various
Python gods through their respective prophets.

Somebody really ought to start a "Python idioms" wiki though...

(Here's where someone chimes in with just such a beast.)

-Peter



More information about the Python-list mailing list