0
I don't really understand how classmethods and static methods work in python. Can anyone please help?
4 ответов
+ 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.
0
All about it here
https://stackoverflow.com/questions/136097/difference-between-staticmethod-and-classmethod#:~:text=Difference%3A%20A%20classmethod%20will%20receive,may%20have%20a%20related%20functionality.
0
"corey schafer" a youtube channel, look there. i learned from there
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.