Python __rxor__() Magic Method

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

Python __xor__() Magic Method

Syntax object.__xor__(self, other) The Python __xor__() method implements the built-in Bitwise XOR ^ operation. So, when you cal x ^ y, Python attempts to call x.__xor__(y). If the method is not implemented, Python first attempts to call __rxor__ on the right operand and if this isn’t implemented either, it raises a TypeError. We call this … Read more

Python __and__() Magic Method

Syntax object.__and__(self, other) The Python __and__() method implements the built-in Bitwise AND & operation. So, when you cal x & y, Python attempts to call x.__and__(y). If the method is not implemented, Python first attempts to call __rand__ on the right operand and if this isn’t implemented either, it raises a TypeError. We call this … Read more

Python __rshift__() Magic Method

Syntax object.__rshift__(self, other) The Python __rshift__() method implements the built-in >> operation. So, when you cal x >> y, Python attempts to call x.__rshift__(y). If the method is not implemented, Python first attempts to call __rrshift__ on the right operand and if this isn’t implemented either, it raises a TypeError. We call this a “Dunder … Read more

Python __lshift__() Magic Method

Syntax object.__lshift__(self, other) The Python __lshift__() method implements the built-in << operation. So, when you cal x << y, Python attempts to call x.__lshift__(y). If the method is not implemented, Python first attempts to call __rlshift__ on the right operand and if this isn’t implemented either, it raises a TypeError. We call this a “Dunder … Read more

Python __pow__() Magic Method

Syntax object.__pow__(self, other) The Python __pow__() method implements the built-in exponentiation operation. So, when you call pow(a, b) or a ** b, Python attempts to call x.__pow__(y). If the method is not implemented, Python first attempts to call __rpow__ on the right operand and if this isn’t implemented either, it raises a TypeError. We call … Read more

Python Namespaces Made Simple

Namespace are everywhere in Python whether you realize it or not. If you don’t know about Python namespaces, you’ll eventually introduce nasty bugs into your Python code. Let’s fix this once and for all! πŸ™‚ As you read over the article, you can watch my explainer video: Why Namespaces? In many classes with 30+ students, … Read more

Python __divmod__() Magic Method

Syntax object.__divmod__(self, other) The Python __divmod__() method implements the built-in divmod operation. So, when you call divmod(a, b), Python attempts to call x.__divmod__(y). If the method is not implemented, Python first attempts to call __rdivmod__ on the right operand and if this isn’t implemented either, it raises a TypeError. We call this a “Dunder Method” … Read more

Python __mod__() Magic Method

Syntax object.__mod__(self, other) The Python __mod__() method implements the modulo operation % that per default returns the remainder of dividing the left by the right operand. Internally, Python attempts to call x.__mod__(y) to implement the modulo operation x%y. If the method is not implemented, Python first attempts to call __rmod__ on the right operand and … Read more

Python __floordiv__() Magic Method

Syntax object.__floordiv__(self, other) The Python __floordiv__() method implements the integer division operation // called floor division—as opposed to the true division operation /. For example to evaluate the expression x // y, Python attempts to call x.__floordiv__(y). If the method is not implemented, Python first attempts to call __rfloordiv__ on the right operand and if … Read more

Python __truediv__() Magic Method

Syntax object.__truediv__(self, other) The Python __truediv__() method is called to implement the normal division operation / called true division—as opposed to the floor division operation //. For example to evaluate the expression x / y, Python attempts to call x.__truediv__(y). We call this a “Dunder Method” for “Double Underscore Method” (also called “magic method”). To … Read more