Implementation principle of dynamic binding of Python classes

When using the instance to reference the properties of the class, dynamic binding occurs. That is, python will bind the corresponding class attribute to the instance every time the instance references the class attribute.

Examples of dynamic binding:

classA:
 def test1(self):print("hello")
  
 def test2(self):print("world")

def bound():
 a =A()
 a.test1()
 A.test1 = A.test2
 a.test1()if __name__ =="__main__":bound()

Output result:

hello2 world

As can be seen from the above code, the change of the class method affects the invocation of the method by the instance in real time, which shows that python dynamically finds the class method in the process of calling the method by the instance.

The cost of dynamic binding:

classA:
 def test(self):
 pass
def one_loop(limited_time):
 a =A()for i inrange(limited_time):
 a.test()
 f = a.test
 for i inrange(limited_time):f()

In the above two loops, one calls a.test() to continuously perform dynamic binding, and the other first assigns a.test to f. There is only one dynamic binding. By timing the two loops, the dynamic binding is tested. The price.

Output result:

1 [0.0, 0.0, 0.0, 0.0, 0.0, 0.0009999275207519531, 0.008995771408081055, 0.19991111755371094, 1.2715933322906494, 15.831915855407715]
2 [0.0, 0.0, 0.0, 0.0, 0.0, 0.12503726671039295, 0.09472344399590288, 0.1999776288967874, 0.131608969147562, 0.1553209370384522]

The abscissa of the line graph is log10 (cycle times), and the ordinate is seconds.

In the output data, the first line is the difference between the time spent in dynamic binding and one-time binding, and the second line is the ratio of the difference in the total dynamic binding time.

It can be seen that when the frequency is very small, there is basically no difference between the two, or the difference is negligible.

After 10^7 cycles, that is tens of millions of cycles, there is a significant difference in the time consumed between dynamic binding and static binding. When the number of cycles reaches one billion, the time-consuming difference is as much as 15 seconds. Accounted for 15% of the total time.

It can be seen from the above that the efficiency of dynamic binding is lower than that of static binding, but since the cost of binding is very time-consuming, there is basically no effect when the number of times is small.

Advantages of dynamic binding:

classA:
 def test_hello(self):print("hello")

def test_world(self):print("world")

def main():
 s =A()
 # Early binding
 f = s.test_hello
 # Change method
 A.test_hello = test_world
 f()
 # Dynamic binding
 s.test_hello()if __name__ =="__main__":main()

Output result:

hello2 world

The changes of the class method can be reflected in the dynamic binding in real time, and the changes of the class method cannot be perceived by the early binding.

to sum up:

  1. The cost of one-time dynamic binding is very small. When the number of binding times is small, the efficiency is basically not affected, and the effect will be significant when the number of binding times reaches tens of millions.

  2. Dynamic binding real-time tracking class method changes, more flexibility.

The above is the whole content of this article, I hope it will be helpful to everyone's study.

Recommended Posts

Implementation principle of dynamic binding of Python classes
Python implementation of gomoku program
Detailed implementation of Python plug-in mechanism
Implementation of reverse traversal of python list
Python implementation of IOU calculation case
Python preliminary implementation of word2vec operation
Implementation of python selenium operation cookie
Implementation of python3 registration global hotkey
Implementation of python student management system
Implementation of python gradient descent algorithm
Implementation of JWT user authentication in python
Python implementation of intersection and IOU tutorial
Detailed explanation of the principle of Python super() method
7 features of Python3.9
Implementation of Python headless crawler to download files
Python implementation of AI automatic matting example analysis
Python implementation of hand drawing effect example sharing
Implementation of business card management system with python
Python writes the game implementation of fishing master
Detailed explanation of the principle of Python function parameter classification
Detailed explanation of the principle of Python timer thread pool
Implementation of business card management system based on python
Detailed explanation of the implementation steps of Python interface development
Basics of Python syntax
Basic syntax of Python
Basic knowledge of Python (1)
Prettytable module of python
09. Common modules of Python3