Constructors
Special Methods
These are methods in Python classes that begin and end with double underscores (__
), also known as “dunder” methods. They define how objects of the class behave in certain contexts. They’re crucial for creating classes that interact seamlessly with built-in Python features and other libraries.
Examples and Functionality:
__init__(self, ...)
: The constructor. Called when you create an instance of the class. Used for initializing attributes.
class Dog:
def __init__(self, name, breed):
self.name = name
self.breed = breed
my_dog = Dog("Buddy", "Golden Retriever")
__str__(self)
: Defines how the object is represented as a string (usingstr(my_dog)
). Crucial for readability.
class Dog:
# ... __init__ ...
def __str__(self):
return f"Dog named {self.name}, breed: {self.breed}"
print(my_dog) # Calls __str__
__repr__(self)
: Defines how the object is represented unambiguously (usingrepr(my_dog)
). Intended for developers; should be unambiguous and ideally allow recreating the object.
class Dog:
# ... __init__ ... __str__ ...
def __repr__(self):
return f"Dog('{self.name}', '{self.breed}')"
print(repr(my_dog)) # Calls __repr__
__len__(self)
: Defines the length of the object (usinglen(my_object)
). Useful for custom sequence-like classes.
class MyList:
def __init__(self, data):
self.data = data
def __len__(self):
return len(self.data)
my_list = MyList(1,2,3)
print(len(my_list))
-
__add__(self, other)
: Defines the behavior of the+
operator for objects of your class. Operator Overloading -
__eq__(self, other)
: Defines the behavior of the==
operator. Operator Overloading -
__iter__(self)
: Makes your class iterable (usingfor
loops). Needs to return an iterator object. Iterators and Generators -
__getitem__(self, key)
: Allows accessing items using indexing (my_object[index]]
). Context Managers
Further Exploration:
- Operator Overloading: Focuses on
__add__
,__eq__
,__mul__
, etc. How to customize arithmetic and comparison Operators. - Iterators and Generators: Detailed explanation of how to create iterable classes using
__iter__
and__next__
. - Context Managers: Explanation of
__enter__
and__exit__
methods for managing resources. - Object Lifecycle: A deeper dive into object creation, destruction, and garbage collection. How these special methods interact with the Python interpreter.
This is not an exhaustive list, but covers some of the most frequently used special methods. The Python documentation provides a complete reference.