Code examples on how to bind properties and methods between classes and instances in python

黄舟
Release: 2017-08-20 10:54:02
Original
1634 people have browsed it

I have been learning python recently, purely as a hobby. However, I have not systematically read python programming books. I feel that the above description is too cumbersome, so I have found some learning websites on the website. The following article mainly introduces to you about For information on how to bind attributes and methods between classes and instances in Python, friends who need it can refer to it.

Preface

I feel confused when calling methods of python classes and instances. After thinking about it, I record my thoughts. To deepen your understanding, consolidate your memory, and help some friends who want to learn Python understand this abstract language. Since Python is a dynamic language, classes and instances created based on classes can be arbitrarily bound to attributes and methods. They are introduced below. .

1. Class binding attributes

Class binding attributes can be defined directly in the class. This attribute is a class attribute.


 class Student(object):
  name = 'Student'
Copy after login

Although this attribute is classified as owned, all instances of the class can be accessed.


class Student(object):
 name = 'Student'
s = Student() # 创建实例s
print(s.name) # 打印name属性,因为实例并没有name属性,所以会继续查找class的name属性
print(Student.name) # 打印类的name属性
Student
Student
Copy after login

If you modify the value of s.name at this time, there will be the following results:


s.name = 'xiaoming' # 给实例绑定name属性
print(s.name) # 由于实例属性优先级比类属性高,因此,它会屏蔽掉类的name属性
print(Student.name) # 但是类属性并未消失,用Student.name仍然可以访问
xiaoming
Student
Copy after login

Next delete s.name attribute:


del s.name # 如果删除实例的name属性
print(s.name) # 再次调用s.name,由于实例的name属性没有找到,类的name属性就显示出来了
Student
Copy after login

It can be seen that the instance attribute with the same name will overwrite the class attribute. After deleting the instance attribute, the instance will access the class attribute upwards.

2. Instance binding properties

There are two ways to bind properties to instances, one is through the self variable of the class, and the other is Assign values ​​directly to instances.


class Student(object):
 def __init__(self, name):
  self.name = name
s = Student('Bob')#方法一 通过类的self变量绑定属性
s.score = 90#方法二 直接赋值
Copy after login

3. Class binding method

There are two types of class binding methods, first The shape is like binding attributes to a class, and the routine is as follows:


Class Student(object):
 pass
a=Student()#创建实例

def set_score(self,score):
 self.score=score

Student.set_score=set_score#类绑定方法
a.set_score(99)#调用方法
a.score
99#输出
Copy after login

The second is to use MethodType to bind methods to the class:


Class Student(object):
 pass
a=Student()#创建实例

def set_score(self,score):
 self.score=score

from types import MethodType
Student.set_score = MethodType(set_score, Student)

a.set_score(99)#调用方法
a.score
99#输出
Copy after login

There is one thing to note about this method. If you continue to create an instance b:


b=Student()
b.set_score(60)
b.score
a.score
60
Copy after login

you will find that the attribute score value of a also becomes 60. . My personal understanding here is that the score here is not directly bound to the class like the previous method, but is similar to a shared reference relationship like a list,

That is, both instances a and b refer to this score as Its own properties, and when it is modified, the corresponding properties of all instances that reference it will change together.

4. Instance binding method

The first method is to bind the method to the class, which can be called by the instance, as shown above.

The second is to use MethodType to bind a method to a single instance.


Class Student(object):
 pass
a=Student()#创建实例

def set_score(self,score):
 self.score=score

from types import MethodType
a.set_score = MethodType(set_score, a)

a.set_score(99)#调用方法
a.score
99#输出
Copy after login

Note that this method only works for instance a. If you need all instances of class Studnet to be callable, then just bind the method directly to class Student.

Summarize

The above is the detailed content of Code examples on how to bind properties and methods between classes and instances in python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template