9.1. Object-oriented: static methods, class methods, and attribute methods, object-oriented
It is considered that the method is of the same meaning as the function. Because method is used here, it is called a method.
Static Method:
- Use @ staticmethod to define static methods.
- Static Method: a method that can be called by both the class and the instance. In fact, it has nothing to do with the class. For such unrelated methods, use static methods. [There is no difference between calling an instance and a class]
Class Dog (object): def _ init _ (self, name): self. name = name def talk (self): print ("% s is talking" % self. name) @ staticmethod def eat (self, food): # print ("% s is eating % s" % (self. name, food) @ staticmethod def bulk (): # if no instance variable is involved, do not pass self print ("wang! ") D = Dog (" haha ") d. talk () Dog. eat (d, "baozi") d. eat (d, "mianbao") d. bulk () Dog. bulk ()
Note: because it is not relevant, self will not be automatically transferred. If the operation involves object content, you need to manually input
Class method:
- Use @ classmethod to define class methods
- Class methods can only contain class variables and cannot access instance variables.
- If many class variables are involved, but few instance variables are involved, use the class method.
Class Dog (object): name = "haha" def _ init _ (self, name): self. name = name @ classmethod def eat (self): print (self) # <class '_ main __. dog '> indicates that self is a print ("% s is eating" % self. name) # So only named = Dog ("aotuman") d in the class will be called. eat () # haha is eatingDog. eat () # haha is eatingd2 = Dog ("huluwa") d2.eat ()
Note: The self in the class method is a class, so only the class variables will be modified.
Attribute method:
- @ Property changes a method to an attribute.
- Why is there a property method: [Reference: http://python.jobbole.com/81967 /]
Current requirement: if I enter a factor, you will print the calculation result.
Use variables to implement:
Disadvantage: the assignment cannot be restricted. If you want to restrict the input of non-numbers, you must create a function if you want to restrict the assignment. This is actually the same as the attribute method, however, it is much more difficult to detect in my_sum than to create a function]
Use attribute methods:
Explanation:
- @ Property is used for obtaining; @ setter is used for assigning values to pass parameters; @ deleter is used to delete attributes and does not affect other functions unrelated to the current @ property.
- To associate the three functions, the function names must be the same and the prefix is the name of the function modified by @ property.
class calc: @property def my_sum(self): print(self.one+self.two) @my_sum.setter def my_sum(self,l): if isinstance(l[0],int)&isinstance(l[1],int): self.one=l[0] self.two=l[1] else: print("no int") @my_sum.deleter def my_sum(self): print("del done") del self.one,self.twoc=calc()c.my_sum=[1,2]c.my_sumdel c.my_sum