Special methods
Special Methods¶
Special methods in Python, often referred to as magic methods or dunder methods, short for double underscore, are methods that have double underscores at the beginning and end of their names. These methods allow you to define the behavior of objects for built-in operations.
SOME Key Functions of Special Methods in Python:¶
Object Initialization and Destruction¶
__init__(self, ...)
: Initializes a new object.__new__(cls, ...)
: Creates a new instance of a class.__del__(self)
: Defines behavior for object destruction.
String Representation¶
__str__(self)
: Returns a string representation of the object for end users.__repr__(self)
: Returns a string representation of the object for developers.__format__(self, format_spec)
: Defines behavior for theformat()
function.__bytes__(self)
: Returns a bytes representation of the object.
Comparison Operators¶
__eq__(self, other)
: Defines behavior for the equality operator (==
).__ne__(self, other)
: Defines behavior for the inequality operator (!=
).__lt__(self, other)
: Defines behavior for the less-than operator (<
).__le__(self, other)
: Defines behavior for the less-than-or-equal-to 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 true division operator (/
).__floordiv__(self, other)
: Defines behavior for the floor division operator (//
).__mod__(self, other)
: Defines behavior for the modulus operator (%
).__pow__(self, other)
: Defines behavior for the exponentiation operator (**
).
Unary Operators¶
__neg__(self)
: Defines behavior for the unary negation operator (-
).__pos__(self)
: Defines behavior for the unary positive operator (+
).__abs__(self)
: Defines behavior for theabs()
function.__invert__(self)
: Defines behavior for the bitwise inversion operator (~
).
Bitwise Operators¶
__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 (^
).__lshift__(self, other)
: Defines behavior for the bitwise left shift operator (<<
).__rshift__(self, other)
: Defines behavior for the bitwise right shift operator (>>
).
In-place Operators¶
__iadd__(self, other)
: Defines behavior for the in-place addition operator (+=
).__isub__(self, other)
: Defines behavior for the in-place subtraction operator (-=
).__imul__(self, other)
: Defines behavior for the in-place multiplication operator (*=
).__itruediv__(self, other)
: Defines behavior for the in-place true division operator (/=
).__ifloordiv__(self, other)
: Defines behavior for the in-place floor division operator (//=
).__imod__(self, other)
: Defines behavior for the in-place modulus operator (%=
).__ipow__(self, other)
: Defines behavior for the in-place exponentiation operator (**=
).__iand__(self, other)
: Defines behavior for the in-place bitwise AND operator (&=
).__ior__(self, other)
: Defines behavior for the in-place bitwise OR operator (|=
).__ixor__(self, other)
: Defines behavior for the in-place bitwise XOR operator (^=
).__ilshift__(self, other)
: Defines behavior for the in-place bitwise left shift operator (<<=
).__irshift__(self, other)
: Defines behavior for the in-place bitwise right shift operator (>>=
).
Container Methods¶
__len__(self)
: Returns the length of the container.__getitem__(self, key)
: Defines behavior for element retrieval using the[]
operator.__setitem__(self, key, value)
: Defines behavior for element assignment using the[]
operator.__delitem__(self, key)
: Defines behavior for element deletion using the[]
operator.__iter__(self)
: Returns an iterator for the container.__reversed__(self)
: Returns a reverse iterator for the container.__contains__(self, item)
: Defines behavior for thein
operator.
Context Management¶
__enter__(self)
: Defines behavior for entering a runtime context.__exit__(self, exc_type, exc_value, traceback)
: Defines behavior for exiting a runtime context.
Attribute Access¶
__getattr__(self, name)
: Defines behavior for attribute retrieval when the attribute is not found.__setattr__(self, name, value)
: Defines behavior for attribute assignment.__delattr__(self, name)
: Defines behavior for attribute deletion.__getattribute__(self, name)
: Defines behavior for attribute retrieval.__dir__(self)
: Returns a list of attribute names.
Descriptor Protocol¶
__get__(self, instance, owner)
: Defines behavior for attribute retrieval.__set__(self, instance, value)
: Defines behavior for attribute assignment.__delete__(self, instance)
: Defines behavior for attribute deletion.
Callable Objects¶
__call__(self, ...)
: Defines behavior for calling an object as a function.
Type Conversion¶
__int__(self)
: Defines behavior for conversion to an integer.__float__(self)
: Defines behavior for conversion to a float.__complex__(self)
: Defines behavior for conversion to a complex number.__bool__(self)
: Defines behavior for conversion to a boolean.
Pickling¶
__getstate__(self)
: Defines behavior for pickling an object.__setstate__(self, state)
: Defines behavior for unpickling an object.
Copying¶
__copy__(self)
: Defines behavior for shallow copying.__deepcopy__(self, memo)
: Defines behavior for deep copying.
Class Methods¶
__init_subclass__(cls, **kwargs)
: Initializes a subclass.__class_getitem__(cls, item)
: Defines behavior for class-level[]
operator.
Other Methods¶
__instancecheck__(self, instance)
: Defines behavior forisinstance()
.__subclasscheck__(self, sub)
: Defines behavior forissubclass()
.__hash__(self)
: Defines behavior forhash()
.__index__(self)
: Defines behavior for conversion to an integer for indexing.