Python binding method and non-binding method
This article provides examples of Python binding and non-binding methods for your reference. The details are as follows:
Definition:
Binding method (to whom it is bound, and who calls it will automatically pass it as the first parameter ):
1. Method of binding to a class: the method of decorating with classmethod.
Tailored for categories
Class. boud_method (), automatically pass the class as the first parameter
(In fact, the object can also be called, but the class is still passed as the first parameter)
2. Method of binding to an object: The method is not decorated by any decorator.
Tailored for objects
Object. boud_method (), which is automatically passed in as the first parameter
(The class function can be called, but it must follow the function rules. If the value is not automatically transferred, it should be described as follows)
Non-binding method: Method for decorating with staticmethod
It is not bound to a class or object. Both classes and objects can be called, but values are not automatically transferred. It's just a common tool.
Note: functions directly defined in the class are separated from the method bound to the object. functions not decorated by any decorator are all bound to the object, but not common functions, the object will automatically pass the value when calling this method, and the staticmethod does not automatically pass the value for whatever method you call.
Ii. Binding method
Method for binding to an object (omitted)
Method bound to the class (classmethod)
Classmehtop is used for the class, that is, it is bound to the class, when a class is used, the class itself is passed as a parameter to the first parameter of the class method (even if an object is called, the class is passed as the first parameter ), python has built-in function classmethod for us to define functions in the class as class methods
Import settingsclass MySQL: def _ init _ (self, host, port): self. host = host self. port = port @ classmethod def from_conf (cls): print (cls) return cls (settings. HOST, settings. PORT) print (MySQL. from_conf) # <bound method MySQL. from_conf of <class '_ main __. mySQL '> conn = MySQL. from_conf () conn. from_conf () # The object can also be called, but the first parameter passed by default is still a class
Iii. Non-binding method
Functions decorated with staticmethod in the class are non-binding methods and common functions.
Statimethod is not bound to a class or object and can be called by anyone without the effect of automatically passing values.
Import hashlibimport timeclass MySQL: def _ init _ (self, host, port): self. id = self. create_id () self. host = host self. port = port @ staticmethod def create_id (): # is a common tool m = hashlib. md5 (str (time. time ()). encode ('utf-8') return m. hexdigest () print (MySQL. create_id) # <function MySQL. create_id at 0x0000000001E6B9D8> # check that the result is a normal function conn = MySQL ('123. 0.0.1 ', 3306) print (conn. create_id) # <function MySQL. create_id at 0x00000000026FB9D8> # Check the result as a normal function.
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.