Code Explanation:
1️⃣ Defining the Class
class A:
Explanation
A class named A is created.
It will have special string representation methods.
2️⃣ Defining __str__
def __str__(self):
return "STR"
Explanation
__str__ defines human-readable representation.
Used when:
print(a)
str(a)
3️⃣ Defining __repr__
def __repr__(self):
return "REPR"
Explanation
__repr__ defines official / developer representation.
Used in:
lists
debugging
interactive shell
4️⃣ Creating Object
a = A()
Explanation
Creates an instance a of class A.
5️⃣ Printing Inside List
print([a])
Explanation ⚠️ IMPORTANT
When printing a list:
Python uses __repr__, NOT __str__
๐ So internally:
repr(a)
Which returns:
"REPR"
๐ค Final Output
['REPR']

0 Comments:
Post a Comment