Python development [Chapter 7]: Python object-oriented advanced, python object-oriented
1. Static Method
Through @ staticmethod, you can change the decoration method to a static method. What is a static method? In fact, it is not hard to understand that a common method can be called directly after instantiation, and self can be used in the method. call instance variables or class variables, but the static method does not allow access to instance variables or class variables. A method that does not allow access to instance variables and class variables, in fact, it has nothing to do with the class itself. Its unique association with the class is to call this method through the class name.
# Static method class Schoolmate (object): def _ init _ (self, name): self. name = name @ staticmethod # convert the eat Method to the static method def eat (self): print ("% s is eating" % self. name) p = Schoolmate ("LianZhiLei") p. eat () # Traceback (most recent call last): # File "C:/Users/L/PycharmProjects/s14/class/Day7/staticmothod. py ", line 16, in <module> # p. eat () # TypeError: eat () missing 1 required positional argument: 'Self'
The above call will produce the following error, saying that the eat needs a self parameter, but it is not passed during the call. Yes, when the eat changes to a static method, when you call an instance, the instance itself is not automatically passed to self as a parameter.
There are two ways to make the above Code work normally
- The instance itself is automatically transferred to the eat method during the call, that is, p. eat (p)
- Remove the self parameter in the eat method, but this also means that other variables in the instance cannot be called through self. In the eat method.
# Static method class Schoolmate (object): def _ init _ (self, name): self. name = name @ staticmethod # convert the eat Method to the static method def eat (self): print ("% s is eating" % self. name) p = Schoolmate ("LianZhiLei") p. eat (p) # LianZhiLei is eating # static method class Schoolmate (object): def _ init _ (self, name): self. name = name @ staticmethod # convert the eat Method to the static method def eat (): print ("is eating") p = Schoolmate ("LianZhiLei") p. eat () # is eating
2. Class Methods
The class method is related to the class as its name implies. The class method is implemented through the @ classmethod modifier. The difference between the class method and the common method is that the class method can only be a class variable and cannot access the instance variable.
# Class method class Schoolmate (object): def _ init _ (self, name): self. name = name @ classmethod # change the eat Method to the class method def eat (self): print ("% s is eating" % self. name) p = Schoolmate ("LianZhiLei") p. eat () # Traceback (most recent call last): # File "C:/Users/L/PycharmProjects/s14/class/Day7/classmothod. py ", line 15, in <module> # p. eat () # File "C:/Users/L/PycharmProjects/s14/class/Day7/classmothod. py ", line 12, in eat # print (" % s is eating "% self. name) # AttributeError: type object 'schoolmate 'has no attribute 'name'
The following error is reported: schoolmat does not have the name attribute. This is because name is an instance variable, and class methods cannot access instance variables. Only class variables are allowed.
# Class method class Schoolmate (object): name = ("Schoolmat class variable") def _ init _ (self, name): self. name = name @ classmethod # change the eat Method to the class method def eat (self): print ("% s is eating" % self. name) p = Schoolmate ("LianZhiLei") p. eat () # The class variable of Schoolmat is eating
In this case, you can define a class variable named name.
3. Attribute Methods
The role of the attribute method is to convert a method into a static attribute through @ property. This is very useful and will be involved in Later courses. Let's look at the code first.
# Attribute method class Schoolmate (object): name = ("Schoolmat class variable") def _ init _ (self, name): self. name = name @ property # convert the eat Method to the attribute method def eat (self): print ("% s is eating" % self. name) p = Schoolmate ("LianZhiLei") p. eat # Traceback (most recent call last): # File "C:/Users/L/PycharmProjects/s14/class/Day7/property. py ", line 17, in <module> # p. eat () # TypeError: 'nonetype 'object is not callable
The following error occurs during the call: NoneType is not callable. Because eat has now become a static attribute, it is not a method. If you want to call eat, you do not need to add a () number. Directly d. eat.
# Attribute method class Schoolmate (object): name = ("Schoolmat class variable") def _ init _ (self, name): self. name = name @ property # convert the eat Method to the attribute method def eat (self): print ("% s is eating" % self. name) p = Schoolmate ("LianZhiLei") p. eat # LianZhiLei is eating
Well, what is the use of turning a method into a static property? If you want static variables, isn't it possible to define them as a static variable? Well, in the future, we will encounter situations where static variables are not fixed and cannot be implemented simply by defining static attributes. For example, if you want to know the current status of a flight, you must take the following steps to know whether the status has arrived, delayed, canceled, or flown away: connect to the airline API query, parse the query results, and return the results to your users. Therefore, the value of this status attribute is the result obtained after a series of actions, so each time you call, in fact, it takes a series of actions to return your results, but these actions do not need to be concerned, the user only needs to call this attribute, understand?
# Attribute Method Instance class Flight (object): def _ init _ (self, name): self. flight_name = name def checking_status (self): print ("checking flight % s status" % self. flight_name) return 1 @ property def flight_status (self): status = self. checking_status () if status = 0: print ("flight got canceled... ") elif status = 1: print (" flight is arrived... ") elif status = 2: print (" flight has departured already... ") else: print (" cannot confirm the flight status ..., please check later ") f = Flight (" CA980 ") f. flight_status
Cool, now I can only query the flight status. Since this flight_status is already an attribute, can I assign a value to it? Try it.
f = Flight("CA980")f.flight_statusf.flight_status = 2# checking flight CA980 status # Traceback (most recent call last):# flight is arrived...# File "C:/Users/L/PycharmProjects/s14/class/Day7/flight_status.py", line 31, in <module># f.flight_status = 2# AttributeError: can't set attribute
Output, saying that this attribute cannot be changed. I will wipe it... what should I do... Of course you can change it, but you need to use the @ proerty. setter modifier to decorate it. Now you need to write a new method to change this flight_status.
# Attribute method class Flight (object): def _ init _ (self, name): self. flight_name = name def checking_status (self): print ("checking flight % s status" % self. flight_name) return 1 @ property def flight_status (self): status = self. checking_status () if status = 0: print ("flight got canceled... ") elif status = 1: print (" flight is arrived... ") elif status = 2: print (" flight has departured already... ") else: print (" cannot confirm the flight status ..., please check later ") @ flight_status.setter # def flight_status (self, status): status_dic = {0:" canceled ", 1:" arrived ", 2: "departured"} print ("\ 033 [31; 1 mHas changed the flight status to \ 033 [0 m", status_dic.get (status) @ flight_status.deleter # Delete def flight_status (self): print ("status got removed... ") f = Flight (" CA980 ") f. flight_status = 0 # Trigger @ flight_status.setter only executes the setter decoration code del f. flight_status # Trigger @ flight_status.deleter to only execute the code for er Decoration
Execute the corresponding operation to trigger the corresponding decorator. At this time, the original attribute will not be triggered. Only the code below the decorator will be executed. You need to perform the corresponding operation to add it to the code block (modify, delete); It is triggered, and the decorator does not perform any operations.
4. Special member methods of the class