Python learning path: staticmethod classmethod property method,
Reference: http://www.cnblogs.com/alex3714/articles/5213184.html
Static Method
Only nominal Classification Management. In fact, the static method cannot access any attributes of the class or instance.
Class Dog (object): def _ init _ (self, name): self. name = name @ staticmethod # It actually has nothing to do with the class def eat (self): print ("% s is eating % s" % (self. name, 'dd') def talk (self): print ("% s is talking" % self. name) d = Dog ("ChenRH") d. eat (d) d. talk ()
Class method:
Only class variables are allowed. instance variables cannot be accessed.
Class Dog (object): # n = 3333 name = "Huazai" def _ init _ (self, name): self. name = name # @ staticmethod # It actually has nothing to do with the class. @ classmethod def eat (self): print ("% s is eating % s" % (self. name, 'dd') def talk (self): print ("% s is talking" % self. name) d = Dog ("ChenRH") d. eat () d. talk ()
Attribute method:
Convert a method into a static property
Class Dog (object): # n = 3333 name = "Huazai" def _ init _ (self, name): self. name = name # @ staticmethod # It actually has nothing to do with the class # @ classmethod @ property # attribute def eat (self ): print ("% s is eating % s" % (self. name, 'dd') # def talk (self): # print ("% s is talking" % self. name) d = Dog ("ChenRH") d. eat
Class Dog (object): # n = 3333 name = "Huazai" def _ init _ (self, name): self. name = name self. _ food = None # @ staticmethod # It actually has nothing to do with the class # @ classmethod @ property # attribute, turning a method into the attribute def eat (self ): print ("% s is eating % s" % (self. name, self. _ food) @ eat. setter # modify attributes def eat (self, food): print ("set to food:", food) self. _ food = food @ eat. deleter # Delete the private attribute def eat (self): del self. _ food print ("deleted") # def talk (self): # print ("% s is talking" % self. name) d = Dog ("ChenRH") d. eatd. eat = "baozi" del d. eat
Use Cases of static attributes:
If you want to know the current status of a flight, including arrival, delay, cancellation, or flight, you must take the following steps to know the status:
1. Connect to the airline API Query
2. parse the query results
3. Return the result to your user
Therefore, the value of this status attribute is the result obtained after a series of actions. Therefore, each time you call the status attribute, it actually requires a series of actions to return your results, however, these actions do not need to be concerned by the user. The user only needs to call this attribute. Do you understand?
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 # modify 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_statusf.flight_status = 2 # Trigger @ flight_status.setterdel f. flight_status # Trigger @ flight_status.deleter