+ 1

Plz explain this python code?

class Myclass: def __init__(self,a=0,b=0): self.a=a self.b=b @classmethod def val(cls,value): return cls(value*10) obj=Myclass(1,2).val(3) print(obj.a,obj.b)

1st Oct 2020, 7:55 AM
Jeevan Manjunath Naik
Jeevan Manjunath Naik - avatar
1 Odpowiedź
0
Myclass(1, 2) return a Myclass instance with a = 1 and b = 2. Then use this instance to call a classmethod val(3), which returns cls(value*10). cls refers to the constructor of the class, so it returns Myclass(value*10). In this case it's Myclass(30). Now obj is Myclass(30), which a = 30 and b = 0(default value).
1st Oct 2020, 8:43 AM
你知道規則,我也是
你知道規則,我也是 - avatar