
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Define Multiple Python Classes in a Single File
Yes, it is recommended to define multiple Python classes in a single file.
If we define one class per file, we may end up creating a large number of small files, which can be difficult to keep track of. Placing all the interrelated classes in a single file increases the readability of the code.
If multiple classes are not interrelated, we can place them in different files (improves maintainability and scalability).
What is a Python class?
In Python, a class is a collection of objects. It is the blueprint from which objects are being created. It is a logical entity that contains some attributes and methods. Following is an example of a Python class -
class Tutorialspoint: print("Welcome to Tutorialspoint.") obj1 = Tutorialspoint()
Multiple classes in a Single File
In Python, a file with a .py extension is known as a module. A single module/file may contain multiple classes.
Example
Following is an example of multiple classes in a single file -
#tutorialspoint.py class Tutorialspoint: def __init__(self): print("Welcome to Tutorialspoint.") def Method1(self): print("Welcome to Python Tutorial.") class Python(Tutorialspoint): def __init__(self): print("Welcome to Python Programming.") def Method2(self): print("Python is an interpreted language.") class Java(Tutorialspoint): def __init__(self): print("Welcome to Java Tutorial.") Obj1 = Java() Obj1.Method1()
Following is the output of the above code -
Welcome to Java Tutorial. Welcome to Python Tutorial.