tags:

views:

255

answers:

4

I'm quite new to Python in general.

I'm aware that I can create multiple classes in the same .py file, but I'm wondering if I should create each class in its own .py file.

In C# for instance, I would have a class that handles all Database interactions. Then another class that had the business rules.

Is this the case in Python?

+10  A: 

No. Typical Python style is to put related classes in the same module. It may be that a class ends up in a module of its own (especially if it's a large class), but it should not be a goal in its own right. And when you do, please do not name the module after the class -- you'll just end up confusing yourself and others about which is which.

Thomas Wouters
Coming from Java I had this same question, boy was I confused for a while. :-)
lewisblackfan
Interestingly, PyDev gives you the option to do just that.
Uri
+1  A: 

Each .py file represents a module, so you should keep logical groups of functions, constants and classes together in the same file.

Each class in a .py file will just create epic bloat in your module table, since if you're only interested in one class you can still

from whatever import SomeClass
Richo
Python style usually uses `lowercase` for module names and `CapWords` for class names: http://www.python.org/dev/peps/pep-0008/
Greg Hewgill
You're absolutely right Greg, I'll edit.
Richo
+1  A: 

Probably not. Python files are "modules". Modules should contain just what code is independently reusable. If that comprises several classes, which is the norm, then that's perfectly ok.

Triptych
A: 

Another point worth mentioning, is that if a file grows too large, you can always transform it into a package, making easy to reorganize without breaking the client's code.

Bruno Oliveira