Analysis of the use of MethodType in Python learning Day14 python

Analysis of the use of MethodType in python

MethodType: 
Use MethodType to bind a method to a class. Instead of writing the method directly into the class, it creates a link in memory to point to the external method. This link will also be copied when an instance is created.

Case 1: Binding a method to an instance of a class

class Student(object):
    pass

def set_name(self, name):
    self.name = name

s1 = Student()
s2 = Student()
s3 = Student()
 from types import MethodType #Bind
 the set_name method to the s1 instance 
s1.set_name = MethodType(set_name, s1)
s2.set_name = MethodType(set_name, s2)
s1.set_name('tom')
s2.set_name('tony')
print(s1.name, ',', s2.name)

result:

tom , tony

>>> print s3.name

Traceback (most recent call last):
  File "<pyshell#23>", line 1, in <module>
    print s3.name
AttributeError: 'Student' object has no attribute 'name'

Analysis: When MethodType binds a method to a class instance, each instance has its own separate pointing area without interfering with each other.

Case 2: Binding a method to a class

class Student(object):
    pass

def set_name(self, name):
    self.name = name

s1 = Student()
s2 = Student()
s3 = Student()
 from types import MethodType #Bind
 the set_name method to the s1 instance 
Student.set_name = MethodType(set_name,Student)
s1.set_name('tom')
s2.set_name('tony')
print(s1.name, ',', s2.name)

result:

tony , tony

Analysis: When the MethodType binds the method to the class and there is no second None parameter, the instances created through the class will point to the same area, causing the value of the later instance to overwrite the value of the previous instance.

Parse

The Student class itself does not have properties and methods, so instances created with this class also have no properties and methods. Binding the set_age method to the Student class with MethodType does not write the method directly into the Student class, but creates a link in the Student memory that points to the external method, and this link will also be copied when the Student instance is created. So no matter how many instances are created, these instances and the Student class all point to the same set_name method. s1.set_name('tom') does not create the name attribute inside the instance of S1, but creates the name attribute in the memory area of ​​the external set_name method. Because the internal links of s1 and s1 point to the memory area of ​​the external set_name method, regardless of whether s1 or s1 changes the name attribute in the memory area of ​​the set_name method when calling the set_name method, s2 changes s1 and also changes. An instance C will also have a name attribute without calling the set_name method, because the link of C points to the memory area of ​​the set_name method, and set_name has been called by A or B before.

Summarize

It probably means that the properties set by this binding method are a bit like static variables in java

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324974421&siteId=291194637