代码中‘object has no attribute’报错原因及模类实现疑问
Hey there! Let's work through your questions about building the ModuloZ class and fix that error you're seeing.
Should each method return a ModuloZ instance?
Absolutely. The whole point of creating a modulo class is to encapsulate the modulo system—so any operation (addition, multiplication, etc.) should return another member of that system, not a raw integer. For example, when you add two elements in modulo 5, the result should still be a ModuloZ object tied to modulo 5, not just a number. This keeps your operations consistent and preserves the context of the modulo system throughout your code.
Do you need a __repr__ function?
100% yes—plus you might want a __str__ too!
__repr__gives an "official" debug-friendly string representation of your object, likeModuloZ(modulo=5, number=3). This is super helpful when you're debugging and need to see exactly what your object contains.__str__can provide a more user-friendly output, like3 mod 5, which is nicer for printing or displaying to users.
Why the "object has no attribute" error?
Looking at your code snippet, the most likely culprit is that you're trying to operate on a ModuloZ instance with a non-ModuloZ object (like a regular integer). For example, if you run:
a = ModuloZ(5, 2) a + 3 # 3 is an int, not a ModuloZ
The __add__ method tries to access b.number, but b here is an integer which has no number attribute—hence the error.
Another possible issue is if you forgot to properly return a ModuloZ instance (your code cuts off at return ...), but the attribute error is way more common here.
Fixed Example Code
Here's a polished version of your class that addresses all these points:
class ModuloZ(object): def __init__(self, modulo, number): self.modulo = modulo # Initialize by immediately taking the modulo to keep number in valid range self.number = number % modulo def __add__(self, b): # Handle cases where b is an integer, convert to ModuloZ first if isinstance(b, int): b = ModuloZ(self.modulo, b) # Ensure both operands use the same modulo if self.modulo != b.modulo: raise ValueError("Can't perform addition on different modulo systems!") total = self.number + b.number # Return a new ModuloZ instance with the result return ModuloZ(self.modulo, total) def __mul__(self, b): # Same pattern for multiplication if isinstance(b, int): b = ModuloZ(self.modulo, b) if self.modulo != b.modulo: raise ValueError("Can't perform multiplication on different modulo systems!") product = self.number * b.number return ModuloZ(self.modulo, product) def __repr__(self): return f"ModuloZ(modulo={self.modulo}, number={self.number})" def __str__(self): return f"{self.number} mod {self.modulo}"
This code:
- Ensures numbers are always in the valid 0 to modulo-1 range on initialization
- Handles operations with both
ModuloZinstances and regular integers - Checks that operands use the same modulo (you don't want to add mod 5 and mod 7 elements!)
- Includes both
__repr__and__str__for clarity - Returns new
ModuloZinstances for all operations
内容的提问来源于stack exchange,提问作者Fred

