Reply content:
common methods, static methods, and class methods
The original text of this answer is difference between @staticmethod and @classmethod in Python
The content here is the translation of the text that I informed the original author and got permission to
This is the address of my blog post Pyhton static methods and class methods
The most common method in a class is an instance method, that is, a method that passes an instance as the first parameter.
As an example, a basic example approach would be to the following:
class Kls(Object): def __init__( Self, Data): Self.Data = Data def Printd( Self): Print( Self.Data)Ik1 = Kls(' Arun ')Ik2 = Kls(' Seema ')Ik1.Printd()Ik2.Printd()
Read a lot of explanations, I feel this is more clear
Oop-python @classmethod and @staticmethod for beginner?
@classmethod Means:when This method was called, we pass the class as the first argument instead of the instance of that CL (as we normally do with methods). This means your can use the class and its properties inside that method rather than a particular instance.
@staticmethod Means:when This method is called, we don ' t pass a instance of the class to it (as we normally does with meth ODS). This means your can put a function inside a class but you can ' t access the instance of that class (this was useful when your method does not use the instance).
Poll the highest answer to compare the basis, slightly advanced to see below StackOverflow answer, answer TOP1 and TOP2
Oop-python @classmethod and @staticmethod for beginner?
Both classes and instances are objects.
So they can have methods.
Class is called a method of class.
Instance is called an instance method.
As for the static method is written in the class method, must use the class to invoke (very few cases use, generally in the global directly writes the function) @ Libaoban teacher writes already extremely good, Xie invites, but I do not engage in incompatible matter. The previous answer has explained in detail the usage of Classmethod and Staticmethod, which is not mentioned here. Add the usage scenario.
Learn oop, used java,c++ of Staticmethod should be more familiar with, is static method.
Static methods are the same as in normal non-class method, except that namespaces are inside classes. The general usage scenario is a class-related operation, but does not depend on or alter the state of the class or instance.
A more extreme example:
class Utility(object): @staticmethod def list_all_files_in_dir(dir_path):
I don't make the answer, I'm just the porter of the answer ~ ~ ~
1, First look:
What are the specific uses of Classmethod and Staticmethod in Python? -Libaoban's answer
It's clear: what @classmethod and @staticmethod are.
2. Look again:
Oop-python @classmethod and @staticmethod for beginner?
Understand: When to use, how to use.
3. Continue:
Oop-python @classmethod and @staticmethod for beginner?
The answer is a supplement to 2.
Understand: Why do you use it so much? Python instance methods, static methods, class methods
from random import choiceCOLORS = ['Brown','Black','Golden']class Animal(object): def __init__(self,color): self.color = color @classmethod def make_baby(cls): color = choice(COLORS) print cls return cls(color) @staticmethod def speak(): print 'Rora'class Dog(Animal): @staticmethod def speak(): print "Bark!" @classmethod def make_baby(cls): print "making dog baby!" print cls return super(Dog,cls).make_baby()class Cat(Animal): passd = Dog('Brown')print d.colorpup = d.make_baby()pupprint pup.color注意 static method 和 class method 在继承中的区别