1. Introduction: From Definition to Access
In previous topics, we learned that when defining a class, we use the self keyword to create instance variables and instance methods. Instance methods can access and manipulate those specific instance variables.
Once a class blueprint is compiled and objects are instantiated, the next critical step is knowing how to access those instance variables and invoke their instance methods from outside the class.
2. Reviewing the Student Class Architecture
To demonstrate accessing attributes and methods, we utilize the Student class. This blueprint packages three instance variables and three instance methods:
- Three instance variables:
self.name, self.age, self.grade (identified as instance variables because each is prefixed with self. inside the constructor).
- Three instance methods:
__init__(self, name, age, grade) — the constructor
display_info(self) — displays all three instance variables
is_eligible(self) — uses an if-else condition to evaluate whether the student's age is greater than 15
We identify them as instance methods because each accepts self—the object on which the method is called—as its first parameter.
class Student:
# 1. Constructor initializing instance attributes
def __init__(self, name, age, grade):
self.name = name
self.age = age
self.grade = grade
# 2. Instance method to display all details
def display_info(self):
print("Name:", self.name)
print("Age:", self.age)
print("Grade:", self.grade)
# 3. Instance method evaluating admission eligibility
def is_eligible(self):
if self.age > 15:
print(f"{self.name} is eligible for admission.")
else:
print(f"{self.name} is not eligible for admission.")
When this class cell is executed in Jupyter Notebook, Python registers the template into memory.
3. Instantiating Multiple Distinct Objects
From this single template, we instantiate two independent student objects: student1 and student2:
# Instantiating student1: Name Ali, Age 16, Grade 10th
student1 = Student("Ali", 16, "10th")
# Instantiating student2: Name Sara, Age 14, Grade 8th
student2 = Student("Sara", 14, "8th")
Both objects now exist in memory with their respective attributes configured.
4. Accessing Instance Variables Directly
To read an object's instance variable directly from outside the class, use the dot operator (.) by writing the object name, followed by a dot, followed by the attribute name:
# Accessing student1's name attribute
print("Student 1 Name:", student1.name)
# Accessing student2's grade attribute
print("Student 2 Grade:", student2.grade)
Output:
Student 1 Name: Ali
Student 2 Grade: 8th
- Referencing
student1.name retrieves "Ali", the specific name assigned to student1.
- Referencing
student2.grade retrieves "8th", the grade belonging specifically to student2.
5. Invoking Instance Methods
Just like attributes, instance methods are called using the dot operator (.) followed by parentheses ().
Example 1: Calling Methods on student1
# Calling display_info() to output all instance variables
student1.display_info()
# Calling is_eligible() to evaluate admission eligibility
student1.is_eligible()
Execution & Output:
Name: Ali
Age: 16
Grade: 10th
Ali is eligible for admission.
student1.display_info() prints all three instance variables: Ali's name, age (16), and grade (10th).
student1.is_eligible() inspects self.age. Because Ali's age is 16 (which is greater than 15), the if condition evaluates to True, printing that Ali is eligible for admission.
Example 2: Calling Methods on student2
# Calling both methods on student2
student2.display_info()
student2.is_eligible()
Execution & Output:
Name: Sara
Age: 14
Grade: 8th
Sara is not eligible for admission.
student2.display_info() displays Sara's information: Name Sara, Age 14, and Grade 8th.
student2.is_eligible() checks whether Sara's age is greater than 15. Because her age is 14 (less than 15), the condition fails, printing that Sara is not eligible for admission.
6. Why This Pattern Represents Good Programming Practice
Instance variables and methods are bound directly to their respective objects. This encapsulation ensures that each object's data remains completely independent and isolated. Interacting with student1 never alters or interferes with student2, maintaining clean data separation across complex software systems.