Dunder Methods

Python __imul__() Magic Method

Syntax object.__imul__(self, other) The Python __imul__() magic method implements in-place multiplication x *= y that subtracts the operands from each other and assigns the result to the left operand. This operation is also called augmented arithmetic assignment. The method simply returns the new value to be assigned to the first operand. When you call x …

Python __imul__() Magic Method Read More »

Python __isub__() Magic Method

Syntax object.__isub__(self, other) The Python __isub__() magic method implements in-place subtraction x -= y that subtracts the operands from each other and assigns the result to the left operand. This operation is also called augmented arithmetic assignment. The method simply returns the new value to be assigned to the first operand. When you call x …

Python __isub__() Magic Method Read More »

Python __iadd__() Magic Method

Syntax object.__iadd__(self, other) The Python __iadd__() magic method implements in-place addition x += y that adds together the operands and assigns the result to the left operand. This operation is also called augmented arithmetic assignment. The method simply returns the new value to be assigned to the first operand. When you call x += y, …

Python __iadd__() Magic Method Read More »

Python __rrshift__() Magic Method

Syntax object.__rrshift__(self, other) The Python __rrshift__() method implements the reverse bitwise right-shift operation with reflected, swapped operands. So, when you call x >> y, Python attempts to call x.__rshift__(y). If the method is not implemented, Python attempts to call y.__rrshift__(x) on the right operand and if this isn’t implemented either, it raises a TypeError. The …

Python __rrshift__() Magic Method Read More »

Python __rlshift__() Magic Method

Syntax object.__rlshift__(self, other) The Python __rlshift__() method implements the reverse bitwise left-shift operation with reflected, swapped operands. So, when you call x << y, Python attempts to call x.__lshift__(y). If the method is not implemented, Python attempts to call y.__rlshift__(x) on the right operand and if this isn’t implemented either, it raises a TypeError. The …

Python __rlshift__() Magic Method Read More »

Python __rpow__() Magic Method

Syntax object.__rpow__(self, other) The Python __rpow__() method implements the reverse exponentiation operation that is exponentiation with reflected, swapped operands. So, when you call x ** y, Python attempts to call x.__pow__(y). Only if the method is not implemented on the left operand, Python attempts to call __rpow__ on the right operand and if this isn’t …

Python __rpow__() Magic Method Read More »

Python __rdivmod__() Magic Method

Syntax object.__rdivmod__(self, other) The Python __rdivmod__() method implements the divmod() built-in function with reflected, swapped operands. So, when you call divmod(x, y), Python attempts to call x.__divmod__(y). If the method is not implemented, Python attempts to call __rdivmod__ on the right operand. Only if this isn’t implemented either, it raises a TypeError. We call this …

Python __rdivmod__() Magic Method Read More »

Python __rmod__() Magic Method

Syntax object.__rmod__(self, other) The Python __rmod__() method implements the reverse modulo operation with reflected, swapped operands. So, when you call x % y, Python attempts to call x.__mod__(y). If the method is not implemented, Python attempts to call __rmod__ on the right operand and if this isn’t implemented either, it raises a TypeError. We call …

Python __rmod__() Magic Method Read More »

Python __rfloordiv__() Magic Method

Syntax object.__rfloordiv__(self, other) The Python __rfloordiv__() method implements the reverse floor (integer) division operation with reflected, swapped operands. So, when you call x // y, Python attempts to call x.__floordiv__(y). If the method is not implemented, Python attempts to call __rfloordiv__ on the right operand and if this isn’t implemented either, it raises a TypeError. …

Python __rfloordiv__() Magic Method Read More »

Python __rtruediv__() Magic Method

Syntax object.__rtruediv__(self, other) The Python __rtruediv__() method implements the reverse true division operation with reflected, swapped operands. So, when you call x / y, Python attempts to call x.__truediv__(y). If the method is not implemented, Python attempts to call __rtruediv__ on the right operand and if this isn’t implemented either, it raises a TypeError. We …

Python __rtruediv__() Magic Method Read More »

Python __ror__() Magic Method

Syntax object.__ror__(self, other) The Python __ror__() method implements the reverse Bitwise OR | operation with reflected, swapped operands. So, when you call x | y, Python attempts to call x.__or__(y). If the method is not implemented, Python attempts to call __ror__ on the right operand and if this isn’t implemented either, it raises a TypeError. …

Python __ror__() Magic Method Read More »

Python __rmatmul__() Magic Method

Syntax object.__rmatmul__(self, other) The Python __rmatmul__() method implements the reverse matrix multiplication @ operation with reflected, swapped operands. So, when you call x @ y, Python attempts to call x.__matmul__(y). If the method is not implemented, Python attempts to call __rmatmul__ on the right operand and if this isn’t implemented either, it raises a TypeError. …

Python __rmatmul__() Magic Method Read More »