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

PythonServer Side ProgrammingProgramming

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.

Published on 29-Jan-2018 12:11:53