Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: one single module (file .py) for each class? [closed]

I've started programming in python 2 weeks ago. I'm making a separate file (module) for each class as I've done before in languages like Java or C#. But now, seeing tutorials and code from other people, I've realized that many people use the same files to define more than 1 class and the main function but I don't know if they do it like that because are just examples or because it's a python convention or something like that (to define and group many classes in the same files).

So, in Python, one file for each class or many classes in the same files if they can be grouped by any particular feature? (like motor vehicles by one side and just vehicles by the other side).

It's obvious that each one has his own style, but when I ask, I hope general answers or just the conventions, anyway, if someone wants to tell me his opinion about his own style and why, feel free to do it! ;)

like image 907
Drumnbass Avatar asked Apr 28 '15 08:04

Drumnbass


People also ask

Can a single Python file have multiple classes?

In Python there is rule of one module=one file. In Python if you restrict yourself to one class per file (which in Python is not prohibited) you may end up with large number of small files – not easy to keep track. So depending on the scenario and convenience one can have one or more classes per file in Python.

Should each class have its own file?

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.

How many classes can be defined in a single Python file?

The rule is this: a module is the unit of reuse. Everything in Python libraries and other Python applications is either a module or a package of modules. There is no limit on how many classes one can put in a file or a module.

Should classes be in separate files Python?

A module can consist of multiple classes or functions. As Python is not an OO language only, it does not make sense do have a rule that says, one file should only contain one class. One file (module) should contain classes / functions that belong together, i.e. provide similar functionality or depend on each other.


1 Answers

one file for each class

Do not do this. In Java, you usually will not have more than one class in a file (you can, of course nest).

In Python, if you group related classes in a single file, you are on the safe side. Take a look at the Python standard library: many modules contain multiple classes in a single file.

As for the why? In short: Readability. I, personally, enjoy not having to switch between files to read related or similar code. It also makes imports more concise.

Imagine socketserver.py would spread UDPServer, TCPServer, ForkingUDPServer, ForkingTCPServer, ThreadingUDPServer, ThreadingTCPServer, BaseRequestHandler, StreamRequestHandler, DatagramRequestHandler into nine files. How would you import these? Like this?

from socketserver.tcp.server import TCPServer
from socketserver.tcp.server.forking import ForkingTCPServer
...

That's plain overhead. It's overhead, when you write it. It's overhead, when you read it. Isn't this easier?

from socketserver import TCPServer, ForkingTCPServer

That said, no one will stop you, if you put each class into a single file. It just might not be pythonic.

like image 158
miku Avatar answered Nov 01 '22 16:11

miku