+ 1
What is the output?
Class mycls: def__init__(self,a=0,b=0) self.a=a self.b=b @classmethod def value_a(cls,value): return cls (value*10) Obj=mycls(1,2).value_a(3) print(obj a,obj b)
2 Réponses
+ 4
30 0
class mycls:
def __init__(self,a=0,b=0):
self.a=a
self.b=b
@classmethod
def value_a(cls,value):
return cls (value*10)#equivalent to mycls(value*10),so you lost your self.b when using this class method
obj=mycls(1,2).value_a(3)
print(obj.a,obj.b)#obj.b has no value because you reset the obj
btw,remember to not capitalised clasd,I have brushed up the codes a bit
0
Thanks Shiny Star Noted