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)

22nd Jun 2020, 6:30 AM
Zhengrong Yan
Zhengrong Yan - avatar
2 Antworten
+ 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.
22nd Jun 2020, 7:30 AM
Russ
Russ - avatar
+ 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).
22nd Jun 2020, 7:42 AM
SSki11
SSki11 - avatar