0
is there any wrong
class first: def __init__(self,color,number): self.color=color self.num=number def __sub__(self, other): return "\n".join([self.color,other.color]) obj=first("hello",24) var=first("world",12) print(obj.color-var.color)
2 Respostas
+ 2
class first:
def __init__(self,color,number):
self.color=color
self.num=number
def __sub__(self, other):
return "\n".join([self.color,other.color])
obj=first("hello",24)
var=first("world",12)
print(obj-var)
__sub__ is the magic method for subtraction between two objects, not two attributes.
+ 2
Agreed with Russ . __sub__ or any other magic method work on objects and not on their properties.
So at the end, instead of print(obj.color - var.color) you should have written print(obj - color).