tags:

views:

131

answers:

4

I'm very new to Python (I'm coming from a JAVA background) and I'm wondering if anyone could help me with some of the Python standards. Is it a normal or "proper" practice to put multiple class in a module? I have been working with Django and started with the tutorials and they place their database model classes in the same module. Is this something that is normally done or should I stick with 1 class per module? Is their a reason I would do one over the other?

Hope I'm being clear and not to generic. Thanks to everyone in advance!

+3  A: 

It is absolutely proper to do so. A module groups related functionality. If that functionality is implemented in several classes (e.g., Tree, Node, Leaf) then it is appropriate to place them together.

A module is more closely associated with a Java package than a Java class. You can also implement a module as a folder, named for the module, with an __init__.py file inside (so Python can identify the module as such; the __init__.py may also optionally include initialization code and lists of classes, functions, and sub-packages to export.)

Jeff Ober
Note, most style standards have `__init__.py` being empty or close to empty. It turns out it can be confusing and feel disorganized to have modules that contain both submodules and normal stuff at the same levels.
Mike Graham
@Mike Graham: django places lots of functions and classes in `__init__.py` files and django seems to be well-regarded.
blokeley
@blokeley, Django is certainly popular, but I don't know if it is widely regarded as adhering to Python best principles. I have never used Django myself, but it sounds like it requires people to do a log of ugly, awful things to get their apps working. See http://jcalderone.livejournal.com/39794.html and http://effbot.org/pyfaq/what-is-init-py-used-for.htm and various SO threads to read about `__init__.py` usually being emptyish. Projects in my site-packages that do this include Twisted, PyMeta, and PyFlakes.
Mike Graham
+2  A: 

It is certainly a normal thing to do in Python. When and way you choose one over the other is partly a matter of taste, and partly convention.

If you're still getting to know Python, and therefore its conventions, reading the style guide is well worth your time.

acrosman
+1  A: 

When in doubt, just look at Python's standard libraries :)

For example, the standard calendar module contains 31 classes. So yes, it is ok.

Goose Bumper
+7  A: 

Here is a useful rule of thumb from what I have seen of typical Java projects:

The bottom-most package in Java should be a file in Python

What does that mean? If your Java project was organized:

toplevel/
   subproject/
        Foo.java
        Bar.java
   subproject2/
        Baz.java
        Qux.java

Then your Python project should look like:

toplevel/
    subproject.py <-- put class Foo, Bar here
    subproject2.py <-- put class Baz, Qux here

Things to notice re: organization:

  • Do not use inner classes. Just put classes in the same module
  • By convention, things that start with _ are "private"
  • It's OK to have "public variables"
moshez