16

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! ;)

Drumnbass
  • 867
  • 1
  • 14
  • 35
  • 2
    I think any answer you get is going to be an opinion, there is no definitive answer. – Padraic Cunningham Apr 28 '15 at 08:40
  • 1
    If you want one file per class, organise your class files into a directory and have an `__init__.py` that does `from ClassName import *` for each class. "Never do this" seems a very dismissive answer, like any pattern there are advantages and disadvantages. – szmoore Jan 19 '17 at 10:20

3 Answers3

12

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.

miku
  • 181,842
  • 47
  • 306
  • 310
  • 2
    But why? I mean, what's wrong with having a file for each class or why is it fine to define many classes in same files? – Drumnbass Apr 28 '15 at 08:29
  • 1
    It's not wrong, but it can become a big mess with a lot of imports – Morb Apr 28 '15 at 08:29
  • 2
    I do that with C++ too, I put all related classes together in the same file. Less files to open, less includes, less time to find the class/file I want. – Morb Apr 28 '15 at 08:32
  • 1
    But I thought that if I made a file for each class the code is like.. no more legible but I know in every moment where do I have all classes defined. If I define many classes for each file, probably I will get myself in a mess looking for some particular class. – Drumnbass Apr 28 '15 at 08:32
  • @Morb well... that's right. The only advantage I see is don't mess with circular dependencies between files, but not much else. – Drumnbass Apr 28 '15 at 08:35
  • @Drumnbass, updated my answer with a concrete example. – miku Apr 28 '15 at 08:38
  • Thanks @miku. You won't be switching between files but you will probably use CTRL+F million of times in your code sessions :P but you're right, it's more readable like that. – Drumnbass Apr 28 '15 at 08:52
  • I sens a lack of understanding of how you work in real world Java. You don't care too much how are things stored on the low level (files). Basically you just have a big tree of packages, classes, and members. IDE-s have efficient ways of navigating in that. As of how imports look, you hardly ever see them, as that's also managed by the IDE. Also, for version control, it's certainly better if you don't have lot of stuff in the same file (less merging needed). – ddekany Mar 28 '20 at 16:49
4

Python has the concept of packages, modules and classes. If you put one class per module, the advantage of having modules is gone. If you have a huge class, it might be ok to put this class in a separate file, but then again, is it good to have big classes? NO, it's hard to test and maintain. Better have more small classes with specific tasks and put them logically grouped in as few files as possible.

FObersteiner
  • 22,500
  • 8
  • 42
  • 72
Daniel
  • 42,087
  • 4
  • 55
  • 81
2

It's not wrong to have one class per file at all. Python isn't directly aimed at object oriented design so that's why you can get away with multiple classes per file.

I recommend having a read over some style guides if you're confused about what the 'proper' way to do it is.

I suggest either Google's style guide or the official style guide by the Python Foundation

You can also find more material relating to Python's idioms and meta analysis in the PEP index

bastelflp
  • 9,362
  • 7
  • 32
  • 67
Chris
  • 349
  • 2
  • 9