
In Python, special methods (also known as “magic methods” or “dunder methods” for their double underscores at the beginning and end) are used to implement and customize the behavior of built-in operations. These methods allow your objects to integrate more closely with the Python language. Here is a list of commonly used special methods:
Basic Customization
__new__(cls, […]): Called to create a new instance of the class.
__init__(self, […]): Called after the instance is created.
__del__(self): Called when the instance is about to be destroyed.
__repr__(self): Called by the repr() built-in to get the object’s string representation (for developers).
__str__(self): Called by the str() built-in and by the print function to get the object’s string representation (for end-users).
Comparison Methods
__lt__(self, other): Defines behavior for the less-than operator (<).
__le__(self, other): Defines behavior for the less-than-or-equal-to operator (<=).
__eq__(self, other): Defines behavior for the equality operator (==).
__ne__(self, other): Defines behavior for the inequality operator (!=).
__gt__(self, other): Defines behavior for the greater-than operator (>).
__ge__(self, other): Defines behavior for the greater-than-or-equal-to operator (>=).
Arithmetic Operators
__add__(self, other): Defines behavior for the addition operator (+).
__sub__(self, other): Defines behavior for the subtraction operator (-).
__mul__(self, other): Defines behavior for the multiplication operator (*).
__truediv__(self, other): Defines behavior for the division operator (/).
__floordiv__(self, other): Defines behavior for the floor division operator (//).
__mod__(self, other): Defines behavior for the modulo operator (%).
__pow__(self, other[, modulo]): Defines behavior for the exponentiation operator (**).
__lshift__(self, other): Defines behavior for the left bitwise shift operator («).
__rshift__(self, other): Defines behavior for the right bitwise shift operator (»).
__and__(self, other): Defines behavior for the bitwise AND operator (&).
__or__(self, other): Defines behavior for the bitwise OR operator (|).
__xor__(self, other): Defines behavior for the bitwise XOR operator (^).
Unary Operators and Functions
__pos__(self): Implements unary positive (e.g., +obj).
__neg__(self): Implements unary negative (e.g., -obj).
__abs__(self): Implements function abs().
__invert__(self): Implements bitwise inversion operator.
Type Conversion
__int__(self): Implements type conversion to int.
__float__(self): Implements type conversion to float.
__complex__(self): Implements type conversion to complex.
__bool__(self): Implements type conversion to bool.
Container Types
__len__(self): Implements the built-in function len().
__getitem__(self, key): Implements accessing an element (self[key]).
__setitem__(self, key, value): Implements setting an element (self[key] = value).
__delitem__(self, key): Implements deleting an element (del self[key]).
__iter__(self): Implements iteration over the container.
__reversed__(self): Implements reversed iteration.
__contains__(self, item): Implements the in operator.
Context Managers
enter(self): Enters a runtime context (with statement).
exit(self, exc_type, exc_value, traceback): Exits a runtime context.
Other Special Methods
__call__(self, […]): Allows an instance of a class to be called as a function.
__getattr__(self, name): Called when an attribute lookup has not found the attribute in the usual places.
__setattr__(self, name, value): Called when an attribute assignment is attempted.
__delattr__(self, name): Called when an attribute deletion is attempted.
__hash__(self): Implements function hash(). If eq is defined, hash must be explicitly defined as well.
__copy__(self): Implements shallow copy for the copy module.
__deepcopy__(self, memo): Implements deep copy for the copy module.
These methods allow your objects to mimic and behave like built-in types, making your code more intuitive and Pythonic. However, it’s important to implement them correctly to avoid unexpected behavior.