9. Operators & Syntax control - JulTob/Python GitHub Wiki
⚓️ Python Operator Overloading: A Comprehensive Guide
Here is the list of what you can command for custom classes:
Note
It is not possible to change the number of operands of an operator.
Warning
Not All Operators Are Overloadable:
Logical operators like and, or, and not cannot be overloaded directly. However, you can achieve similar behavior by overloading bool and using bitwise operators (&, |, ~) as substitutes.
🧭 Unary Operators
Symbol
Dunder Method
Description
+a
__pos__(self)
Unary positive
-a
__neg__(self)
Unary negation
~a
__invert__(self)
Bitwise NOT
⚔️ Binary Operators
Symbol
Method
Reflected Method
Description
a + b
__add__
__radd__
Addition
a - b
__sub__
__rsub__
Subtraction
a * b
__mul__
__rmul__
Multiplication
a / b
__truediv__
__rtruediv__
True Division
a // b
__floordiv__
__rfloordiv__
Floor Division
a % b
__mod__
__rmod__
Modulo
a ** b
__pow__
__rpow__
Exponentiation
a << b
__lshift__
__rlshift__
Left bitwise shift
a >> b
__rshift__
__rrshift__
Right bitwise shift
a & b
__and__
__rand__
Bitwise AND
a ⎮ b
__or__
__ror__
Bitwise OR
a ^ b
__xor__
__rxor__
Bitwise XOR
🧙 Reflected methods (__radd__, etc.) are used when the left operand doesn’t know what to do with the right operand’s type. They are called when the left operand does not support the operation and the operands are of different types.
⚖️ Comparison Operators
Symbol
Method
Description
==
__eq__
Equal
!=
__ne__
Not Equal
<
__lt__
Less than
<=
__le__
Less than or equal
>
__gt__
Greater than
>=
__ge__
Greater than or equal
🔁 In-place Operators (Assignment Ops)
Symbol
In-place Method
Description
+=
__iadd__
In-place addition
-=
__isub__
In-place subtraction
*=
__imul__
In-place multiplication
/=
__itruediv__
In-place division
//=
__ifloordiv__
In-place floor division
%=
__imod__
In-place modulo
**=
__ipow__
In-place power
<<=
__ilshift__
In-place left shift
>>=
__irshift__
In-place right shift
&=
__iand__
In-place bitwise AND
│=
__ior__
In-place bitwise OR
^=
__ixor__
In-place bitwise XOR
In-place Methods are used for in-place operations (e.g., a += b). If not defined, Python falls back to the regular method (e.g., add) and assigns the result to the variable.
Functions
Syntax.
Method
Description
def
obj()
__call__
Callable object
str(obj)
__str__
User-facing string
repr(obj)
__repr__
Debug/Dev string
len(obj)
__len__
Length (e.g., for containers)
abs(obj)
__abs__
Absolute value
complex(obj)
__complex__
Convert to complex
int(obj)
__int__
Convert to integer
float(obj)
__float__
Convert to float
bool(obj)
__bool__
Convert to boolean
hash(obj)
__hash__
Hash for sets/dicts
obj = Class()
__init__
Initialize the object.
Numeric
Syntax
Method
Does
round(obj)
__round__
Rounds a numeric object.
math.trunc(obj)
__trunc__
truncate number
math.floor(obj)
__floor__
floor number
math.ceil(obj)
__ceil__
ceil number
.
__index__
used in slicing
Containers
Syntax
Method
Description
def
item in obj
__contains__
Membership check (in)
obj[key]
__getitem__
Indexing
def __getitem__(obj, key):
obj[key] = val
__setitem__
Item assignment
def __setitem__(obj, key, val):
del obj[key]
__delitem__
Item deletion
def __delitem__(obj, key):
Syntax
Method
Does
with obj as alias:
__enter____exit__
Context Manager
Enter
__enter__
Exit
__exit__
Is guaranteed to run after a successful Enter, including on exceptions