using classes across files
amitmund
June 04, 2026
Using Classes Across Files (Modules)
Why Do We Need It?
In real projects, all classes are not written in a single file.
We separate classes into different files to keep code organized and reusable.
student.py
class Student:
def __init__(self, name):
self.name = name
def show(self):
print(self.name)
main.py
from student import Student
s1 = Student("John")
s1.show()
Output:
John
How It Works
Python reads:
from student import Student
Meaning:
- Open the file
student.py - Find the class
Student - Make it available in the current file
Now we can create objects normally.
Visual Representation
student.py
│
└── Student Class
│
▼
main.py
│
├── import Student
│
└── create object
Rule to Remember
One file can contain classes.
Other files can import and use those classes.