Skip to content

Python Tutorial: Special Methods in Classes πŸ§‘β€πŸ’»


Table of Contents πŸ“š

  1. Introduction to Special Methods
  2. The __init__ Method πŸ› οΈ
  3. Example: Initializing Objects
  4. The __str__ and __repr__ Methods πŸ“œ
  5. Example: String Representation of Objects
  6. The __len__ Method πŸ“
  7. Example: Custom Length Calculation
  8. The __getitem__ and __setitem__ Methods 🎯
  9. Example: Indexing and Assignment
  10. The __eq__, __lt__, and Other Comparison Methods βš–οΈ
  11. Example: Custom Comparison
  12. The __call__ Method ☎️
  13. Example: Making Instances Callable
  14. The __del__ Method πŸ—‘οΈ
  15. Example: Cleanup Actions
  16. Summary πŸ“

1. Introduction to Special Methods

Special methods in Python, also known as "dunder" methods (short for double underscores), allow you to define the behavior of your class objects in various situations. These methods start and end with double underscores (e.g., __str__, __init__). By implementing these methods, you can control how your objects are represented, compared, indexed, and more.


2. The __init__ Method πŸ› οΈ

The __init__ method is the constructor in Python. It is called when an instance of the class is created, allowing you to initialize the object's attributes.

Example: Initializing Objects
class Dog:
    def __init__(self, name, age):
        self.name = name
        self.age = age

my_dog = Dog("Buddy", 5)
print(my_dog.name)  # Output: Buddy
print(my_dog.age)   # Output: 5

3. The __str__ and __repr__ Methods πŸ“œ

  • __str__: Defines the human-readable string representation of an object. Used by print() and str().
  • __repr__: Defines the official string representation of an object. Used by repr() and often in interactive shells.
Example: String Representation of Objects
class Dog:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def __str__(self):
        return f"{self.name} is {self.age} years old."

    def __repr__(self):
        return f"Dog('{self.name}', {self.age})"

my_dog = Dog("Buddy", 5)
print(my_dog)  # Output: Buddy is 5 years old.
print(repr(my_dog))  # Output: Dog('Buddy', 5)

4. The __len__ Method πŸ“

The __len__ method allows you to define a custom way to calculate the length of an object, which can be used by the len() function.

Example: Custom Length Calculation
class PackOfDogs:
    def __init__(self, *dogs):
        self.dogs = dogs

    def __len__(self):
        return len(self.dogs)

pack = PackOfDogs("Buddy", "Max", "Bella")
print(len(pack))  # Output: 3

5. The __getitem__ and __setitem__ Methods 🎯

  • __getitem__: Allows your objects to be indexed like lists or dictionaries.
  • __setitem__: Allows you to set values for specific indices.
Example: Indexing and Assignment
class PackOfDogs:
    def __init__(self, *dogs):
        self.dogs = list(dogs)

    def __getitem__(self, index):
        return self.dogs[index]

    def __setitem__(self, index, value):
        self.dogs[index] = value

pack = PackOfDogs("Buddy", "Max", "Bella")
print(pack[0])  # Output: Buddy

pack[1] = "Rocky"
print(pack[1])  # Output: Rocky

6. The __eq__, __lt__, and Other Comparison Methods βš–οΈ

Python allows you to define custom comparison logic by implementing the following methods:

  • __eq__(self, other): Equal to (==)
  • __lt__(self, other): Less than (<)
  • __le__(self, other): Less than or equal to (<=)
  • __gt__(self, other): Greater than (>)
  • __ge__(self, other): Greater than or equal to (>=)
  • __ne__(self, other): Not equal to (!=)
Example: Custom Comparison
class Dog:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def __eq__(self, other):
        return self.age == other.age

    def __lt__(self, other):
        return self.age < other.age

dog1 = Dog("Buddy", 5)
dog2 = Dog("Max", 7)

print(dog1 == dog2)  # Output: False
print(dog1 < dog2)   # Output: True

7. The __call__ Method ☎️

The __call__ method allows an instance of a class to be called as a function.

Example: Making Instances Callable
class Dog:
    def __init__(self, name):
        self.name = name

    def __call__(self, sound):
        return f"{self.name} says {sound}"

my_dog = Dog("Buddy")
print(my_dog("Woof"))  # Output: Buddy says Woof

8. The __del__ Method πŸ—‘οΈ

The __del__ method is called when an object is about to be destroyed. It’s the destructor method in Python, and can be used to clean up resources like closing files or network connections.

Example: Cleanup Actions
class Dog:
    def __init__(self, name):
        self.name = name
        print(f"{self.name} has been created.")

    def __del__(self):
        print(f"{self.name} has been destroyed.")

my_dog = Dog("Buddy")
del my_dog  # Output: Buddy has been destroyed.

9. Summary πŸ“

Special methods, or dunder methods, are a powerful feature in Python that allows you to define the behavior of your objects in various situations. From initialization (__init__) to string representation (__str__ and __repr__), indexing (__getitem__), comparison (__eq__, __lt__), and even callable instances (__call__), these methods give you control over how your objects behave and interact in different contexts.