tags:

views:

376

answers:

4

I have been programming Python for a while and I have a very good understanding of its features, but I would like to improve my coding style. I think reading the source code of the Python Modules would be a good idea. Can anyone recommend any ones in particular?

Related Threads:

+5  A: 

I vote for itertools. You'll learn a lot of functional programming style from using this code, though perhaps not from reading the source.

For a good module-by-module tutorial, try Doug Hellmann's Python Module of the Week. I also like the python programming style/practices explored and developed at WordAligned. I also like Peter Norvig's code, especially the spelling corrector code and the sudoku solver.

Other cool modules to learn: collections, operator, os.path, optparse, and the process/threading modules.

hughdbrown
A: 

I am learning Django and I really like their coding style,

http://www.djangoproject.com/

ZZ Coder
Although there are interesting ideas in django source, there is also a lot of things you should avoid. I especially dislike using globals for everything, which make django inflexible.
Denis Otkidach
I am new to python and looking for a flexible web framework. What's your suggestion?
ZZ Coder
Django's definitely the most popular, but I often find it too magical (hiding too much, doing too much on my behalf); so I often turn to WSGI with lightweight helpers (at most, some subset of Werkzeug). But I guess that's a peculiar style (the server side being more of a webapp than a website, "view" functionality abundantly delegated to javascript+dojo on the browser), and most people prefer richer/thicker server-side frameworks; in that niche Django's main contenders are Pylons/Turbogears2, or at the really rich/heavy extreme end of the spectrum, Zope/Plone.
Alex Martelli
A: 

PEP 8 is the "standard" Python coding style, for whatever version of "standard" you want to use. =)

Benno
+14  A: 

Queue.py shows you how to make a class thread-safe, and the proper use of the Template Method design pattern.

sched.py is a great example of the Dependency Injection pattern.

heapq.py is a really well-crafted implementation of the Heap data structure.

If I had to pick my three favorite modules in the Python standard library, this triplet would probably be my choice. (It doesn't hurt that they're all so very useful... but I'm picking in terms of quality of code, comments and design, first and foremost).

Alex Martelli
Queue is *awesome* when dealing with thread communication.
FogleBird
@FogleBird, agreed. But so is itertools when dealing with iterators... however, there IS no itertools.py, and when somebody's asking for good Python modules to read, http://svn.python.org/view/python/trunk/Modules/itertoolsmodule.c?revision=73536-).
Alex Martelli
@Alex: I did not take the poster to be asking to read C modules for Python implementation. If he's asking to improve his coding style, then presumably he really wants to see good Python code and to learn how to use the modules. At least, that's the way I am taking this request. Looking at the anwsers (which are all over the place), I'd say the question could stand a bit of rewriting or focusing.
hughdbrown
@hughdbrown, I based my take on the question in the subkect ("modules most worthwhile reading", not reading ABOUT;-) and text ("reading the source code of the modules" to improve his style) -- I don't see how the poor OP could have made it any clearer!-) I guess most people just don't read sources for style and pleasure, but at most for usefulness and learning about the modules themselves.
Alex Martelli