- This topic is empty.
-
AuthorPosts
-
February 19, 2026 at 2:00 am #6102
Why
student.exercises_completedWorks Without a Method## 🔹 Initial ContextWhile learning Object-Oriented Programming (OOP) in Python, many beginners encounter code like this:
class Student: def __init__(self, exercises_completed): self.exercises_completed = exercises_completed students = {} students["Alice"] = Student(12) print(students["Alice"].exercises_completed)This works perfectly and prints:
12But learners often ask:
“I never created a method called
exercises_completed.
So how does this line work?”Let’s understand what is really happening.
🔹 Step 1: What Happens Inside
__init__Look at this line inside the constructor:
self.exercises_completed = exercises_completedThis line does something very important:
It creates an attribute on the object.
It means:
“Attach a variable named
exercises_completedto this object.”When you write:
Student(12)Python automatically runs:
self.exercises_completed = 12So the object now stores this value.
🔹 Step 2: Attribute vs Method (Key Difference)
In Python, class members are of two main types:
Type Purpose Example Attribute Stores data student.exercises_completedMethod Performs action student.how_many()Attribute (Data)
print(student.exercises_completed)No parentheses → reading stored data.
Method (Function)
print(student.how_many())Parentheses → calling a function.
🔹 Step 3: What Is
selfReally Doing?Inside the class:
self.exercises_completed = exercises_completedMeans:
self→ the current objectself.exercises_completed→ a variable inside that object
So each
Studentobject gets its own copy.Example:
a = Student(10) b = Student(20)Now:
a.exercises_completed = 10 b.exercises_completed = 20They are independent.
🔹 Step 4: How Python Finds Attributes
When you write:
students["Alice"].exercises_completedPython does:
1️⃣ Get the
Studentobject
2️⃣ Look inside it
3️⃣ Findexercises_completed
4️⃣ Return its valueNo method is involved.
🔹 Step 5: Seeing Attributes Internally with
__dict__Every object stores its attributes in a dictionary called
__dict__.Try this:
s = Student(15) print(s.__dict__)Output:
{'exercises_completed': 15}This proves that
exercises_completedis stored as data.
🔹 Step 6: Using a Method Instead (Optional Design)
Sometimes you may want controlled access:
class Student: def __init__(self, exercises_completed): self.exercises_completed = exercises_completed def get_exercises(self): return self.exercises_completedNow you use:
print(student.get_exercises())This is useful when you want validation later.
🔹 Step 7: Common Beginner Mistake
Many learners think:
“Dot notation always means method.”
Not true.
Dot notation accesses:
✔ Attributes
✔ Methods
✔ PropertiesThe difference is parentheses.
🔹 Step 8: Real-Life Analogy
Think of an object as a student record file.
Inside the file:
Name: Alice Exercises: 12You can read “Exercises” directly.
You don’t need a function to see it.
✅ Final Takeaway
In Python:
Writing
self.variable = valueinside__init__creates an attribute.So:
self.exercises_completed = exercises_completedCreates:
student.exercises_completedWhich you can access directly.
Remember:
✔ Attributes store data
✔ Methods perform actions
✔ No parentheses → attribute
✔ Parentheses → methodUnderstanding this difference is essential for mastering object-oriented programming.
-
AuthorPosts
- You must be logged in to reply to this topic.

