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?