What can I do with a Python class method that I can't do with a Python static method?
-
I understand the syntactical differences, but functionally, what is different? Why would I choose a class method over a static method in a Python class?
-
Answer:
The important difference is: class methods take the class as a first parameter (typically named 'cls'), analogous to the way instance methods take 'self' as the first parameter; static methods don't take any implicit first parameter. Some sources on the Internet claim that static methods in Python can't be overridden by inheritance but I'm pretty sure that this is incorrect. An example demonstrating this: class A(object): @staticmethod def f(): return "A" class B(A): @staticmethod def f(): return "B" class C(B): pass In [3]: A.f() Out[3]: 'A' In [4]: B.f() Out[4]: 'B' In [5]: C.f() Out[5]: 'B'
Charlie Cheever at Quora Visit the source
Other answers
Classmethods take the class on which the method was called as their first argument; this enables some cool polymorphism with classes. Here's one use-case I'm using in production now. I'm using SQLAlchemy and have a base class for my model classes that defines a find() wrapper around sqlalchemy's query(), like this: class BaseModel(object): @classmethod def find(cls, **kw): return session.query(cls).filter_by(**kw).one() class User(BaseModel): pass Since the first argument to the classmethod is the class the method is being called one, I can call User.find() and it's as if I had written the following: class User(BaseModel): @staticmethod def find(**kw): return session.query(User).filter_by(**kw).one() except that I don't have to duplicate the definition of find() for every one of my model classes.
Brian Rue
In a static method, you have to refer to the current class by name. class A(object): @staticmethod def foo(): print 'foo' A.bar() @staticmethod def bar(): print 'bar' class B(A): @staticmethod def bar(): print 'new bar' Now when you call `B.foo()`, the `B.bar` won't override `A.bar` like when we use classmethod.
Satoru Logic
classmethod is an attribute of the class, just like a normal method is an attribute for some instance of class. Its first argument is cls, which like self in normal methods is implicit. Static methods are neither attached to classes or their instances Both static method and class method are put into the 'class type' object at the same time (ie when the code of class is executed), so I think there is no difference in their performances (ie in speed). They both are coexisting just because they can be used for different types of jobs. A classmethod is a cool way to do things with intantiation (the cls argument of it is callable as cls() thus running the __init__ method of class,so overriding in subclasses is possible) while staticmethod is good for dependency injection(using function of some other module in our class thorugh its instances). E.g., >>> class myList(list): def getListFromKeys(cls,dicti): return cls(dicti) getListFromKeys=classmethod(getListFromKeys) >>> a=myList() >>> b=a.getListFromKeys({'a':'apple'}) >>> b ['a'] >>> Static Method is to use some method declred in some other module into our class and use it by the instances of our class. There is no other way to call a method of class with the first argument not being cls or self
Muktabh Mayank
Related Q & A:
- What can I do get a job in a restaurant?Best solution by Yahoo! Answers
- What can I do with a Bachelors in Psychology and a associates in Criminal justice?Best solution by Yahoo! Answers
- I can't get into a yahoo chatroom.Best solution by Yahoo! Answers
- What can I take on a plane and what can't I?Best solution by Yahoo! Answers
- What can I do for a visual aids speech for my speech class?Best solution by best-speech-topics.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.