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 there a reason I would do one over the other?
Hope I'm being clear and not to generic. Thanks to everyone in advance!
-
1Duplicate: stackoverflow.com/questions/106896/…S.Lott– S.Lott2010年04月14日 02:00:40 +00:00Commented Apr 14, 2010 at 2:00
-
I've wondered the same thing. (I'm new to Python with Java experience.) Also, how do packages fit in the Pythonic mind?Eric Wilson– Eric Wilson2010年04月14日 02:03:30 +00:00Commented Apr 14, 2010 at 2:03
5 Answers 5
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"
2 Comments
Think it this way.
In java what you write is a Class where in the case of Python, you write a module instead of a class. So a module can contain several classes. Whenever you want to use a particular class, import the respective module first and then call the class to make objects.
Here's an example.
Classes.py (This is a module named 'Classes')
class MyClass(object):
def greet(self):
print("Hello World")
class MyNextClass(object):
def greetAgain(self):
print("Hello again")
Now I can import this module from anywhere I wish
import Classes
if __name__ == '__main__':
a=Classes.MyClass()
a.greet()
b=Classes.MyNextClass()
b.greetAgain()
Comments
When in doubt, just look at Python's standard libraries :)
For example, the standard calendar module contains 31 classes. So yes, it is ok.
Comments
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.)
3 Comments
__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.__init__.py files and django seems to be well-regarded.__init__.py usually being emptyish. Projects in my site-packages that do this include Twisted, PyMeta, and PyFlakes.It is certainly a normal thing to do in Python. When and why 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.