0
Please explain this code
I have played around with this, but I can't figure out what this code is doing. I'm obviously not understanding how the classmethod works. Thanks for the help! class My class: def __init__(self, a=0, b=0): self.a = a self.b = b @classmethod def value_ax10(cls, value): return cls(value * 10) obj = My class(1,2).value_ax10(3) print(obj.a, obj.b) #prints 30, 0
10 Answers
+ 3
As it was mentioned in SoloLeqrn tutorial, class method allows you to call method from the class with different parameters.
in this case you use function value_ax10 to multiply its parameter (value) by 10.
Then you return the result to the class back (return cls....) so your class's instance now takes this value as a parameter (a). As soon as you provide only one parameter (a), the second parameter (b) is taken by the constructor by default (0, as it is declared in __init__).
Now, your instanse has two parameters passed to the __init__ (constructor) : 30 and 0, which corresponds to a and b respectively.
obj.a and obj.b just print them out.
Check out the snippet below:
class Myclass:
def __init__(self, a=0, b=0):
self.a = a
self.b = b
@classmethod
def value_ax10(cls, value, foo=555):
return cls(value * 10, foo)
obj = Myclass(1,2).value_ax10(3)
===
obj.a is now 30
obj.b is now 555 (foo parameter of the classmethod taken by default).
Simply saying, in this challenge classmethod just calles the Myclass instance overriding its parameters.
+ 2
Could you discribe what you think this code should do and what result you expected?
I am beginner, but it seems that this code missing some parts, compering to what was in the lessons:
https://www.sololearn.com/learn/JUMP_LINK__&&__Python__&&__JUMP_LINK/2473/
+ 2
blackfish I just challenged you. I got none of them correct!
+ 2
Yes. I agree. Not enough time to think through the questions. But then you can read them again after the challenge is finished. And learn from them.
+ 1
blackfish I don't know what it is doing. It came from one of the challenges. I always go through the ones I got wrong and try to learn from them. But I am lost on this one.
+ 1
blackfish It was a multiple choice question. I was supposed to choose which of the four choices would be printed. The correct answer is 30, 0.
When I run the code that is what I get, but I don't know why.
0
But what was the task in this challenge? Find a mistake? Explain why the result is (30, 0) or what? I have not taken part in any challenge so I have no idea how does it work.
0
HZR It is a disaster. I am not at that level to answer any of that questions in such short period of time. I had no time even to go through these codes, not to mentioned to analyse and then solve :-(
But thank you for the invitation. At least I know now how hard it is.
0
strawdog Thanks for your clarification. But then, what for is (1,2) after Myclass? Should it be Myclass.value_ax10(3) only?
0
blackfish
It surely could be Myclass().value_ax10(3), because providing arguments to Myclass is optional in this case. Strictly saying, you can pass any arguments to this class or skip thern at all.