views:

518

answers:

5

What's the best open source Python project for reading code and seeing how Python should be written? I'm at a crossroads in Python; I've written a lot of good code, but I want to write truly exceptional clean and intelligent code. I feel like the best way to do this is to read the source for a good project (if there are other ways, I'm open to these to!).

thanks!

+13  A: 

The Python standard libary of course!

It shipped with your python implementation, is documented very well for the most part and obviously contains some very 'pythonic' code...

ChristopheD
+1: Nothing better. And you already have it.
S.Lott
Handle with caution though, some modules (looking at you mimetypes) are really an abomination.
Alex Gaynor
Indeed, it's quite readable even if your new to Python unlike some other languages STLs such as C++ STL which requires more knowledge to understand.
gradbot
+4  A: 

The code of http://lamsonproject.org is fun to read, well written and not too big.

bayer
+3  A: 

I'm quite fond of Django, but it's fairly large, and I really doubt you could learn as much form just setting down and reading it like prose as you could from using it and reading to see how the stuff you're doing works.

Alex Gaynor
agreed. I was surprised at how far I got when I started investigating the source code. Another plus is that it makes use of multiple paradigms (functional and OO) cleanly. But if you don't get the Django framework to begin with, navigating will be hard. (Also, some they take some appreciated conveniences like bumping common objects up a level by importing them in __init__.py. A good tool in libraries.)
David Berger
I thought about Django, as this is what I use primarily for web applications. However, someone on #python mentioned there's a great deal of magic behind Django (i.e. extracting fieldnames for model.CharField instances from the variable name). This dissuaded me.
Vince
+6  A: 

As an alternative to reading the source code of a python project, read the entries in Doug Hellmann's Python Module of the Week. You'll find that more focused on how to make good use of each python module and learn new ways to do things.

hughdbrown
A: 

First of all, there is no "best" project. There are a lot of good projects but almost any project will have good bits and bad bits, especially if lots of different people contributed to it. The Python library is like that, some great stuff and some really awful stuff.

It's better to look at many different projects, so that you are not reading code under the assumption that it is brilliant, but you are reading code in order to judge it and evaluate it in comparison with other projects. Koders is a good site to do this because it has collected a lot of Python source code.

Another, slightly different approach that I recommend in order to learn how to write excellent code, is to hunt through delicious.com from time to time using python as your search keyword. Look for interesting modules and packages that you might want to use in your own projects, download them, and look through them. Because you have a desire to make use of these projects, you will be more motivated to find out what makes them tick. At the same time you will come across lots of blog postings where people discuss Python, and at least some of these will have code critiques.

A number of sites also cover Python best practices, but you always have to read these critically. For instance PEP 8 will tell you to indent code 4 spaces, but Python works just fine with 2 or 3 space indents, is just as readable, and forces you to break lines less often. Check out sites like the Python Grimoire, Python Best Practices, Idiomatic Python, Python Idioms and Efficiency, and lets not forget that you can search SO using the tags python and best practices.

You can learn a lot from reading bad code by asking yourself, why is it like this? How would I do it better? In what way is my way better? Is there any compelling reason to change this code?

Also, remember that Python's syntax and features have change quite a bit during the journey from Python 2 to Python 3. Code that wants to maintain compatibility with older Python cannot use certain newer best practices. And code that was written a while ago, and which works just fine, may not need to be updated, even if with hindsight you could do it cleaner and slightly more efficiently.

In fact, certain Python idioms that are often touted as being the only right and pythonic way to write code, can make code much harder to understand. As a programmer you have to ask yourself if it is worthwhile being super pythonic if it requires you to write 5 lines of comment to explain what the code does. There is always a delicate balance between several forces at play here.

Michael Dillon
Which Python idiom leads to 5 lines of explanation?
J.F. Sebastian
Python list comprehensions. People look at them as a way to eliminate for loops, and they can be nested. It is too easy to end up with a long multiline list comprehension that would be better left as nested for loops for clarity. With the for loop, the nesting structure is clear and obvious and doesn't require several lines of documentation to explain it.
Michael Dillon