0

I don't really understand how classmethods and static methods work in python. Can anyone please help?

7th Jul 2020, 10:49 PM
Peanuts Berkenshine
Peanuts Berkenshine - avatar
4 odpowiedzi
+ 2
First, you need to understand a regular method, what 'self' means. If that is unclear, read this: https://code.sololearn.com/ce664AekBveN/?ref=app If you call a method via an instance obj, the call will be transformed. obj.f() will internally become YourClass.f(obj). So the method gets the instance as an implicit first argument, which we usually call 'self'. A classmethod works similarly, bit instead of the instance, the class itself will be passed as an argument. That's really the whole difference. A staticmethod doesn't have this behavior, it's just like a regular function and has no implicit first argument. The only difference to a function is that it's sitting in a class.
7th Jul 2020, 11:51 PM
HonFu
HonFu - avatar
7th Jul 2020, 11:15 PM
Ruben Gasparyan
Ruben Gasparyan - avatar
0
"corey schafer" a youtube channel, look there. i learned from there
7th Jul 2020, 11:19 PM
Waleed Mashrequi
Waleed Mashrequi - avatar
0
https://code.sololearn.com/cB22vrwKC3GI/?ref=app I realize 2 ways of one task: initialize a square (classic and classmethod ways). This can help someone.
1st Dec 2020, 5:37 AM
Vitalij Bredun ♡ Python Petersburg
Vitalij Bredun ♡ Python Petersburg - avatar