Are there any performance implications of declaring a class method versus a static method in Python?
-
This is a follow-up question to .
-
Answer:
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 were separatly defined just because they could 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). for eg, >>> 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 declared 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 at Quora Visit the source
Related Q & A:
- How to mock static method call by constructor?Best solution by Stack Overflow
- How to access a non static method in an abstract class's static method?Best solution by Stack Overflow
- How to get actual return type of a generic static method in Java?Best solution by stackoverflow.com
- How do I add to an array from a static method?Best solution by stackoverflow.com
- Is it better to get a higher grade in a easier class or a mediocre/low grade in a hard class?Best solution by greatcollegeadvice.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.