Special Methods
Introduction to OOP's
Class
Object
Constructor
Instance Variables
Class Variables
Instance Methods
self Keyword
Encapsulation
Public Members
Protected Members
Private Members
Inheritance
Single Inheritance
Multiple Inheritance
Multilevel Inheritance
Hierarchical Inheritance
Hybrid Inheritance
super() Function
Polymorphism
Method Overriding
Duck Typing
Abstraction
Abstract Class
Abstract Method
Class Method
Static Method
Special Methods
_str_ Method
Operator Overloading
Composition
Aggregation
Association
Method Resolution Order
Object Class
issubclass() and isinstance()
Property Decorator
Setter Method with @property
Destructor
Inner Class
Complete Mini Example
Special Methods
27. Special Methods
Special methods, also called dunder methods, are methods with double underscores that give custom behavior to objects. Examples include __init__, __str__, and operator-related methods.
Syntax
def __str__(self):
return "text"
Example
class Book:
def __init__(self, title):
self.title = title
def __str__(self):
return self.title
b = Book("Python Basics")
print(b)
Output
Python Basics
Example
Executing...
❌ Error:
✅ Output:
// Click Run ▶ to execute
🏋️ Test Yourself With Exercises
Take our quiz on Special Methods to test your knowledge.
Browse Quizzes »