views:

6984

answers:

15

I have been dabbling in Python for a couple months now, read the online docs/tutorial and started playing with Django a bit as well... and I feel like I am starting to leave my formative toddler years and ready for some more serious code. Most everything I see is generally encapsulated in a single script or so large and unwieldy I don't know where to start.

I would really like to see an excellent example of some proper Python code, style, organization etc. and ideally a WHY associated with some of those decisions. Any ideas for where I should look next? I would prefer a simple console app with at most just a few extra .py files (maybe one package for good measure)...

Oh, and one more thing: The reason this is coming up is I have quite a bit of experience in Java and .NET where it is generally preferred to have one class definition per source file. That rarely seems to be the case in the python world (at least in my very limited exposure) and it just doesn't smell right to me. So maybe this is more Enterprise Python that hobbyist python or something.

+19  A: 

Try Dive into Python, which is available to read online for free.

Brian
A: 

Well, I breezed through the PDF and a few of the sample source files - and like most Python code I encountered, almost all the class definitions and program execution were all lumped into one single .py script. Not quite what I was looking for.

I am completely willing to accept that this is the way most (all?) python code is commonly written, and in my own experience have enjoyed either insanely packed scripts with everything including the kitchen sink or countless import statements at the top of the file to locally scope class/methods. It just seems like I am going about this wrong or perhaps Python isn't really designed with the large app in mind and there will be pain.

Goyuix
+7  A: 

There are plenty of large Python apps, you mention one in your question: Django. Why not browse the Django source to see one way of laying out a larger project.

For laying out classes and modules, one pattern I've seen repeated in bigger Python projects is to put several related classes into a single file, then several related files into a single directory. Put an __init__.py file in that directory and import all of the code in that directory into a single namespace.

An example of this, in the Django source: /trunk/django/forms/

Then, if you wanted to use, say, the CharField class in another file, you could just use

import forms
field = forms.CharField()
Peter Burns
+2  A: 

I've been really impressed with the Python Essential Reference so far. It's one of those great reference books that allows an experienced programmer to quickly learn a new language. The first 120 odd pages are a (fairly) quick rundown of the syntax of the language. The rest of the book is just a reference of the Python Standard Library.

Ben Mills
+2  A: 

It seems that what you are looking for is a something mildly challenging and somewhat useful to do with python? Try some of these things:

Level 1:
Have you tried putting a GUI on top of a simple console program? There are several packages that allow you to do this with minimal effort. Metaslash Covers most all of them.

Level 2:
Google Code Jam. The Competition itself is long underway, but you can still try the qualification round questions. Try doing them in python. They can be done with any amount of complexity you want, one, two, three files. Its a good way to learn to build algorithms in python.

Level 3:
Write your own extension for something in python. I would try maybe a small scaling plugin for GIMP. Documentation can be found here. This will be pretty challenging, and provide you good experience to contributing to open source!

contagious
+1  A: 

Python is such a simple language (once you learn the basics) that generally you don't need to read reams of code to work out how to do things.

I second Brian's suggestion of Dive into Python (and upmodded it to represent my support) to learn the syntax and the pythonic way of doing things and would like to add a link to The Python Library Reference to learn what's available under python.

Keeping things simple works best in the early days and large open source projects rarely serve as simple examples and much simpler stuff isn't as easy to find.

Hang around long enough and I'm sure Stack Overflow will be the resource you want :)

sparkes
+1  A: 

I never really learn to use a language until I try to use it properly for something. It is all well and good going through thesetutorials, but the examples are always written to demonstrate a function of the language when in reality you really want to try to make the language solve your problem. In essence, the best way is to find a problem you need solving and dive right in.

Good luck

Xetius
+20  A: 

You're right that there's no substitute for looking at code, but I've found Code Like a Pythonista to be useful for those kinds of questions. It has small sections on module structure and package organization at the end.

Edit: The Python Tutorial section on modules is also worth reading.

James Sulak
+2  A: 

PEP-8 is the style guide for Python and gives a good guidance on the style and code structure once you know the Python language.

To answer your statement about things being in one big file, I do think that Python tends to do it a little more that way, particularly for libraries, because it's a relatively dense language and the whitespacing makes a large file not feel quite so daunting. Most Python apps I've worked with (and larger libraries like Django, SQLAlchemy, etc.) do break their code up into multiple files and smaller modules. While it's not quite as segmented as Java with its one class per file, there is still a very modular feel to the code. In the end, you should do whatever you feel comfortable with.

Joe Shaw
+1  A: 

Some more advanced features (decorators, generators etc.) once you are beyond the basics common to every language (loops, objects etc.) are also covered in the Google talk Advanced Python or Understanding Python

Brendan
+7  A: 

I have found The Python Challenge a great resource for learning python.

Its an online game in which you go solving riddles using python programming, and you get tons of source code examples, but the condition to see the code examples is that you solve each riddle first. The code examples are really other player's solutions, by the way.

I find that I learn a lot more from the code examples given in the solutions when I already tried to solve myself.

Sergio Acosta
+2  A: 

Generators are one of the coolest new additions to Python.

In order to learn more about them and to see practical code examples of advanced generator usage take a look at this presentation on Generator Tricks for System Programmers.

It was an eye-opener for me.

CaptSolo
+10  A: 

Peter Norvig presents two short and beautiful useful code samples written in Python:

He also recently wrote a book chapter with python code:

namin
Wow! These are two of my favorite online articles and have been for years, and you've picked exactly these two out of all the possibilities!
hughdbrown
+1  A: 

Read the Python Standard Library! Many modules are written in pure Python, and they are readily available on your machine if you have Python installed.

Sure, some of the modules show their age and haven't been updated much along the way, so they might not utilize newer features of the language. But at least the library is written in a very Pythonic style.

akaihola
+3  A: 

i suggest you to start with: http://www.pythonchallenge.com/

because it really shows you the strengths of Python. So you can get a clue that every Language got its own strengths and weaknesses.

bastianneu