0

I'm relatively new at Python programming.

In C++, there may be multiple classes in a single C++ file. But more often than not, I find myself declaring/implementing only one class in each file. In Java, it is mandatory to have only one public class in each file. When I do C++ or Java programming, I usually make the file name very similar to the class contained in the file. For example, if I implement a "Student" class, then I created "Student.h" and "Student.cc".

In Python, it seems like there is no such restriction, but due to my previous habit, I find myself still creating only one class in each python file, and making the file name very similar to the class name. But I think this might look somewhat absurd.

Suppose that we create a class named FrameProcessor in a file named "frame_processor.py". When we use this class in another python file located in the same directory, we need to do as follows:

import frame_processor
def SomeFunction():
   processor = frame_processor.FrameProcessor()

I think having both frame_processor and "FramePrcoessor" looks like a duplication. For me, it looks quite redundant, since the file name frame_processor and the class name FrameProcessor are almost the same.

In short, would it be a good habit to have only one class in each python file?

Qix - MONICA WAS MISTREATED
  • 14,451
  • 16
  • 82
  • 145
chanwcom
  • 4,420
  • 8
  • 37
  • 49
  • Please consult the python naming guide or [PEP 8](https://www.python.org/dev/peps/pep-0008/). As for your module name, it is discouraged to use `_` for a package name. Really though, there is no problems with this or having multiple related classes within a single module. – metatoaster Sep 08 '15 at 03:31
  • 1
    Most larger projects I've come across have multiple related classes inside one source file (module) (often with a class and its subclasses). But if you don't have related classes, just one class, it makes sense to have a module file with just that one clasas. –  Sep 08 '15 at 03:31

0 Answers0