Would you recommend to define multiple Python classes in a single file?


Python is not class-based exclusively - the basic unit of code decomposition in Python is the module. A module is a distinct thing that may have one or two dozen closely-related classes. Modules may as well contain functions along with 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.

Updated on: 09-Sep-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements