GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing zetcode.com — Python __mod__ Method - Complete Guide Python __mod__ MethodLast modified April 8, 2025
This comprehensive guide explores Python's Basic Definitions
The
Key characteristics: it takes two parameters ( Basic __mod__ Implementation
Here's a simple class implementing basic_mod.py
class ModNumber:
def __init__(self, value):
self.value = value
def __mod__(self, other):
return self.value % other
num = ModNumber(17)
result = num % 5
print(result) # Output: 2
This example shows a basic implementation where
The method returns the remainder of division of Modulo with Custom BehaviorWe can customize the modulo operation to implement domain-specific behavior, like circular indexing or custom arithmetic. custom_mod.py
class CircularIndex:
def __init__(self, value):
self.value = value
def __mod__(self, other):
# Implements circular indexing
return self.value % other if other != 0 else 0
index = CircularIndex(7)
print(index % 5) # 2 (7 mod 5)
print(index % 0) # 0 (handle division by zero)
This implementation adds special handling for modulo zero, returning 0 instead of raising an exception. This might be useful in graphics or game programming.
The custom behavior demonstrates how Modulo with Different Types
The mixed_types.py
class TextWrapper:
def __init__(self, text):
self.text = text
def __mod__(self, other):
# String formatting-like behavior
return self.text.replace('%s', str(other))
wrapper = TextWrapper("The answer is %s")
result = wrapper % 42
print(result) # Output: The answer is 42
This example repurposes the
This demonstrates how Reverse Modulo Operation
Python also provides reverse_mod.py
class ModHandler:
def __rmod__(self, other):
return f"Handled modulo: {other}"
handler = ModHandler()
result = 10 % handler
print(result) # Output: Handled modulo: 10
When a regular integer (10) is used with This is useful when you want your custom class to work with built-in types on the right side of the operator. In-Place Modulo Operation
For the inplace_mod.py
class AccumulativeMod:
def __init__(self, value):
self.value = value
def __imod__(self, other):
self.value %= other
return self
num = AccumulativeMod(17)
num %= 5
print(num.value) # Output: 2
This example shows in-place modulo operation. The In-place operations are useful for mutable objects where you want to modify the existing instance rather than creating a new one. Best Practices
Source ReferencesAuthorMy name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming. List all Python tutorials. |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |