What is the difference between @classmethod and @staticmethod in object oriented programming in Python?? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What is the difference between @classmethod and @staticmethod in object oriented programming in Python??

Object oriented Programming

19th Apr 2019, 6:21 AM
Sumit Sinha
Sumit Sinha - avatar
2 Answers
+ 4
instance methods recieve instance as implicit first argument class methods recieve the class as implicit first argument static methods don't recieve any implicit first arguments class A: @staticmethod def static(a, b): print(a, b) @classmethod def clsmeth(cls, a, b): print(cls, a, b) def meth(self, a, b): print(self, a, b) a = A() a.meth(1, 2) #<__main__.A object at 0x77c168c5c0> 1 2 A.meth(a, 1, 2) #<__main__.A object at 0x77c168c5c0> 1 2 a.clsmeth(1, 2) #<class '__main__.A'> 1 2 A.clsmeth(1, 2) #<class '__main__.A'> 1 2 a.static(1, 2) #1 2 A.static(1, 2) #1 2>
19th Apr 2019, 9:31 AM
Mert Yazıcı
Mert Yazıcı - avatar
+ 1
So classmethods and instancemethods have cls and self as arguments but staticmethods have not any arguments. Now I understand. Thanks buddy.
19th Apr 2019, 10:39 AM
Sumit Sinha
Sumit Sinha - avatar