+ 1
Problem in Magic Methods(Python)
class UnderstandingMagicMethod: def __init__(self, other): self.other=other def __mul__(self,another): x="*"*len(another.other) return "\n".join([self.other,x,another.other]) spam = UnderstandingMagicMethod("Hello") eggs = UnderstandingMagicMethod("World") print(spam*eggs) All I expected is: Hello ***** World But I get: TypeError: unsupported operand type(s) for *: 'UnderstandingMagicMethod' and 'UnderstandingMagicMethod' So, could please anyone clarify the problem with my code???
1 Odpowiedź
+ 3
Maybe an indentation problem with __mul__. This works:
----------------------
class UnderstandingMagicMethod:
def __init__(self, other):
self.other=other
def __mul__(self,another):
x="*"*len(another.other)
return "\n".join([self.other,x,another.other])
spam = UnderstandingMagicMethod("Hello")
eggs = UnderstandingMagicMethod("World")
print(spam*eggs)