Details of the dynamic binding code for method properties and method properties of the python class

  • 2020-06-19 10:54:48
  • OfStack

Dynamic languages are different from static languages in many ways. One of the biggest features is that classes and instances can be modified dynamically. In Python, we create a class that can bind to methods or properties of the heart of instances and classes to achieve dynamic binding.

Recently in learning, python is purely his own interests, but did not see python systematically programming books, feel too trival, described on the website to find a learning website, found Liao Xuefeng learning resources is very good, the teacher's website and concise, extraction of 1 some python important grammar and case. It is important to test the running code of python online. The disadvantage is that there is no systematic reading of python's books, and the fragmentation of knowledge cannot be timely linked to one, which is also the difference between reading and not reading. Especially in the method call of python class and instance, I feel confused. After thinking about it, I will write down my thoughts, 1 to deepen my understanding and consolidate my memory, and 1 to help some friends who want to learn python understand this abstract language and understand the improper place. I hope you can give correction, thank you.

1. Classes and instances in python

Let's define a class


class Student(object):

Class to bind 1 of the necessary attributes to the Student class


def __init__(self, name, score):
    self.name = name
    self.score = score

Define a function internally, implement the attribute operation on the incoming instance, and encapsulate the data internally, which is suitable for the methods associated with the class, called classes.


def print_score(self):
    print('%s: %s' % (self.name, self.score))

2. Calls to instance-owned properties and methods

Pass in 1 instance


bart = Student('Bart Simpson', 59)

A call to a property


>>> bart.name
'Bart Simpson'

A call to a method


>>> bart.print_score()
Bart Simpson: 59

No problem, to understand the binding of attributes to methods in an instance of python

3. Instance attributes are bound to methods in python

Let's define a class


class Student(object):
  pass

Pass in 1 instance


s = Student()

Dynamically bind an instance to a property


s.name = 'Michael' 

Next, bind the method to the instance

So let's define a function


def set_age(self, age): #  define 1 Function as an instance method 
  self.age = age

Method binding to an instance


def __init__(self, name, score):
    self.name = name
    self.score = score
0

Method binding to a class


def __init__(self, name, score):
    self.name = name
    self.score = score
1

The set_score method can be directly defined in class, but dynamic binding allows us to dynamically add functionality to class while the program is running, which is difficult to achieve in a static language.

I understand mean, 1 to define a class, it will be initialized in the class attribute of the binding, the incoming instances when direct incoming parameters instance, through internal definition 1 some method, can be directly for instance attributes and inheritance in the class of methods for data operation, references, such as xxx. namexxx. print_name form. However, if the defined class is not initialized, based on the good dynamic binding property of python language, we can bind the instance and the method to the passed instance. The binding to the property is relatively simple, and the binding to the method needs to be in the form of fromtypesimportMethodType (other forms are not known yet). The method that tells the interpreter s.set_age is to bind the set_age function to s, s.set_age =MethodType(set_age,s) so that python knows how to execute set_age. But binding methods in this way can only work on the bound method instances in the class, and binding methods to all instances of the class requires dynamic binding of the class. Just like the one we saw above. After the binding, it can then be called directly in a form similar to xxx.namexxx.print_name.

Whether the method can be called directly depends on whether the defined function is defined in the class or based on the definition of the function. For functions not defined in the class, the implementation of the method operation on the instance requires dynamic binding or method binding on the class to which the instance belongs. Functions defined in a class are methods that can be called directly in an instance.

conclusion

That's the end of the python class's dynamic binding code for method properties and method properties. Interested friends can continue to refer to other related topics in this site, if there is any deficiency, welcome to comment out. Thank you for your support!


Related articles: