In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly shows you "how to achieve bound and unbound methods in python", the content is easy to understand, clear, hope to help you solve your doubts, the following let Xiaobian lead you to study and learn "how to bind and unbound methods in python" this article.
One: binding method: it is characterized by the fact that the caller itself is automatically passed in as the first parameter
1. Method bound to an object: the caller is an object, which is automatically passed in
two。 Method is bound to the class: the caller is the class, and the class is automatically passed in
Import settings_Aclass backend database: def _ _ init__ (self,ip,port): self.ip=ip self.port=port def basic function (self): print ('% slug% s'% (self.ip) Self.port) @ classmethod # decorate the following function as a method bound to the class # get the corresponding parameters from the configuration file to instantiate the basic function of def A (cla): # to create an object The background database of class print (cla) return cla (settings_A.IP, settings_A.PORT) obj1= ('1.1.1.1) print (obj1) print (' ='* 100) print (obj1.__dict__) # corresponding to the output of the parameter obj2= background database. Basic function A () print ('='* 100) print (obj2.__dict__)
The basic contents of settings_A
IP='127.8.2.1'PORT=3306
Effect.
Two: unbound method, static method
No binding: the caller can be a class or object, without the effect of automatically passing parameters
Class background database: def _ init__ (self,ip,port): self.nid=self. Processing of user id () self.ip=ip self.port=port @ staticmethod # decorates the following function as a static method for handling user id (): import uuid#uuid is a random number module return uuid.uuid4 () @ classmethod def function 1 (cls): pass def function 2 (self): passobj1= backstage database ('1.1.1.1') 3306) print (background database. The processing of user id) print (background database. Function 1) print (obj1. Function 2)
Effect.
These are all the contents of the article "how to implement bound and unbound methods in python". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.