English Original: Https://julien.danjou.info/blog/2013/guide-python-static-class-abstract-methods
Translation Source: http://python.jobbole.com/81595/
One, how methods work in Python
A method is a function that is stored as a property of a class. can be declared and accessed in the following form:
In [1]: Class Pizza (Object): ...: def __init__ (self,size): ...: self.size = size ...: def get_size (s ELF): ...: Return self.size ...: in [2]: pizza.get_sizeout[2]: <unbound method pizza.get_size>
The above results tell us that the property of Class pizza Get_size is an unbound method. What does that mean? Will try to get an answer in the following.
In [3]: Pizza.get_size ()---------------------------------------------------------------------------TypeError Traceback (most recent) <ipython-input-3-65dcef60aa06> in <module> ()----> 1 Piz Za.get_size () Typeerror:unbound method Get_size () must be called with Pizza instance as first argument (got nothing instea D
The above results tell us that to invoke the property Get_size of class pizza, we need an instance of pizza. As can be seen from the Get_size method, the first parameter is an instance of a pizza
In [4]: Pizza.get_size (Pizza) out[4]: 42
OK, above we can really go with the class pizza properties get_size. And in real life, we usually use this:
In [5]: Pizza = Pizza (42) # declares an instance of pizza in [6]: Pizza.get_size () # Call pizza method Get_size by declaring an instance out[6]: 42
Second, Static methods
What is a static method? I feel that the original English is more authentic
Static methods is a special case of methods. Sometimes, you'll write code that belongs to a class, but that's doesn ' t use the object itself at all.
Static methods are a special kind of method. Sometimes you want to write code that belongs to a class, but you don't use anything related to that class.
Example:
In [1]: Class Pizza (Object): ...: @staticmethod ...: def mix_ingredients (x, y): ...: return x + Y. .: def Cook (self): ...: Return self.mix_ingredients (Self.cheese,self.vegetables)
In the above example, writing a non-static method can also work. But you need to pass a self argument to the function mix_ingredients, but this parameter is completely unused. The adorner @staticmethod is used here. It brought us a
The following are some of the benefits:
Python can use static methods directly, avoiding the instantiation of an object. Instantiating an object requires resource-intensive, and static methods avoid it.
Readability is better, after seeing @staticmethod, we can know that this method is antagonistic and does not require an instance of a dependent class.
In [3]: Pizza (). Cook was Pizza (). cookout[3]: Falsein [4]: Pizza (). Mix_ingredients is pizza.mix_ingredientsout[4]: Truein [ 5]: Pizza (). Mix_ingredients is Pizza (). mix_ingredientsout[5]: Truein [6]:
As can be seen from the above execution results, static methods can be called by the class, and instances of the same class can be called, but this consumes more resources (looks unprofessional.) haha
Third, Class methods
What is class methods? Class methods is methods that is not the bound to an object, but to ... a class!
What is the time class method, the class method is not bound to an instance of the class, but is bound to the class.
in [1]: class pizza (object): ...: radius = 42 ...: @ Classmethod ...: def get_radius (CLS): ...: return cls.radius ...: in [2]: pizza.get_radiusout[2]: <bound method type.get_radius of <class ' __main__. Pizza ' >>in [3]: pizza (). Get_radiusout[3]: <bound method type.get_radius of <class ' __main__. Pizza ' >>in [4]: pizza.get_radius () out[4]: 42
In either case, the Get_radius is bound to the class Pizza.
So what's the use of this kind of method? When are we going to use it? I think that's what I want to care about.
(1), factory method: It is used to create an instance of a class, such as some preprocessing. If we use @staticmethod instead, we have to hardcode the pizza class name in the function, which makes it impossible for any class inheriting pizza to use this factory method for itself.
Class Pizza (object): Def __init__ (self, ingredients): self.ingredients = Ingredients @classmethod def fro M_fridge (CLS, fridge): Return cls (Fridge.get_cheese () + fridge.get_vegetables ())
(2), Call static class: If you split a static method into multiple static methods, you will have to hardcode the class name unless you use the class method. Declaring a method in this way, the pizza class name will never be directly referenced, inherited, and method overrides work perfectly.
Class Pizza (object): Def __init__ (self, radius, height): Self.radius = radius self.height = height @s Taticmethod def compute_area (RADIUS): Return Math.PI * (RADIUS * * 2) @classmethod def compute_volume (CLS , height, radius): return height * Cls.compute_area (RADIUS) def get_volume (self): return Self.compute_v Olume (Self.height, Self.radius)
This article is from the "Learning Notes" blog, so be sure to keep this source http://unixman.blog.51cto.com/10163040/1664104
Methods in Python, static methods, and class methods