Init_and_Str_Methods_Python
Init_and_Str_Methods_Python
Python has special methods called dunder methods (double underscore), like __init__ and __str__.
- When it runs: Automatically called when you create an object using the class.
Example:
class Student:
self.name = name
self.age = age
s1 = Student("Amit", 15)
print(s1.age) # Output: 15
Notes:
- When it runs: Automatically called when using print() or str() on the object.
Example:
class Student:
self.name = name
self.age = age
def __str__(self):
s1 = Student("Amit", 15)
Notes:
|------------|--------------------------------------|--------------------------------|