In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article introduces the knowledge of "static methods, class methods, attribute methods in Python". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
1. Static method
You can turn its decorating method into a static method through the @ staticmethod decorator. What is a static method? In fact, it is not difficult to understand that ordinary methods can be called directly after instantiation, and can be called through self. Call instance variables or class variables, but static methods cannot access instance variables or class variables. in fact, a method that cannot access instance variables and class variables has nothing to do with the class itself. its only association with the class is that it needs to be called through the class name.
Application:
For a class, to call one of its methods, we must bind the instance, not directly through the class name. Called as the method name (). Therefore, if you want to call a method through a class instead of an instance, you can use the static method @ staticmethod and the class method @ classmethod.
# static methods are only nominally classified and managed. In fact, you cannot access any property in a class or instance in a static method: class Dog (object): def _ init__ (self,name): self.name=name@staticmethod # actually has nothing to do with the class itself, but is nominally a class method (independent of the class Cannot access any properties and methods in the class) def eat (self,name): print ("% s is eating% s"% (self.name,name)) d=Dog ('sb') # d.eat (' baozi') # error because eat requires a self parameter but is not passed when it is called, yes, because when eat becomes a static method When called through the instance, the instance itself will not be automatically passed to self as a parameter # solution # 1. When calling, actively pass the instance itself to the eat method # remove the self parameter from eat, but this also means that you cannot pass self. Other variables in the instance have been called d.eat (djingrabaozi') # execution result > sb is eating baozi2. Class method
Class methods are implemented through the @ classmethod decorator. The difference between class methods and ordinary methods is that class methods can only access class variables, not instance variables.
Class Dog (object): name='SB' # class variable def _ init__ (self,name): self.name=name@classmethod # class methods can only access class variables, not instance variables def eat (self,name): print ('% s is talking% s% (self.name,name)) def talk (self,something): print ("% s is talking% s"% (self.name,something) d=Dog ('Lulu') d.eat (' Mantou') # execution result SB is eating Mantou3. Summary of static methods and class methods
A static method is actually a method defined in a class, but it can be called directly with the class without instantiating the class first. No matter how it is inherited later, its implementation remains the same.
The class method also does not need to instantiate the class first when calling, but its implementation follows the current subclass when inheriting (because its first parameter is always cls)
They are often used to encapsulate data preprocessing in a class to prevent code from spreading outside the class to be difficult to maintain.
4. Attribute method
The function of a property method is to turn a method into a static property through @ property
A: there are no answers to problems encountered in study. The editor has created a Python learning exchange QQ group: 857662006 look for like-minded partners to help each other, and there are also good video learning tutorials and PDF e-books in the group! Class Dog (object): name='suantou'def _ init__ (self,name): self.name=name@property # turns a method into a static property def eat (self): print ('% s is eating% slots% (self.name,'something')) @ eat.setter # assigns def eat (self,food): print ('set to food',food) def talk (self,something) to the eat attribute: print ("% s is talking% s"% (self.name) Something) d=Dog ('Lulu') # d.eat (' baozi') error saying NoneType is not callable, because eat has become a static attribute at this time It's not a method. If you want to call it, you don't need to add a () sign. Just d.eat it.
Actual scenario application:
For example, if you want to know the current status of a flight, whether it has arrived, delayed, cancelled, or has flown away, you must go through the following steps:
Connect to airline API query
Parse the query results
Return the results to your users
Therefore, the value of this status attribute is the result of a series of actions, so every time you call it, it has to go through a series of actions to return your result, but these actions do not need to be concerned by the user, the user only needs to call this attribute.
Class Flight (object): def _ init__ (self, name): self.flight_name = namedef checking_status (self): print ("checking flight% s status"% self.flight_name) return 1@propertydef 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] 1mHas changed the flight status to\ 033 [0m ", 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 Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.