Details on how to bind attributes and methods to classes and instances in python, and python examples

Source: Internet
Author: User
Tags abstract language

Details on how to bind attributes and methods to classes and instances in python, and python examples

Preface

When calling python and instance methods, you may feel confused. After thinking, you can record your thoughts to deepen your understanding and consolidate your memory, to help some friends who want to learn python understand this abstract language, because Python is a dynamic language, classes and instances created based on classes can be bound to any attributes and methods. The following describes the methods.

1. Class binding attributes

Class binding attributes can be defined directly in the class, which is a class.

 class Student(object):  name = 'Student'

Although this attribute is classified as all, all instances of the class can access it.

Class Student (object): name = 'student's = Student () # create an instance sprint (s. name) # print the name attribute. Because the instance does not have the name attribute, the print (Student. name) # print the name attribute of the class StudentStudent

If you modify the value of s. name, the following result is displayed:

S. name = 'xiaoming' # bind the name attribute print (s. name) # because the instance property has a higher priority than the class property, it will block the print (Student. name) # But the class property does not disappear, use Student. name can still access xiaomingStudent

Next, delete the s. name attribute:

Del s. name # If you delete the name attribute print (s. name) # Call s again. name, because the Instance name attribute is not found, the class name attribute is displayed as Student

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

2. instance binding attributes

There are two ways to bind attributes to an instance. One is to use the class self variable, and the other is to directly assign values to the instance.

Class Student (object): def _ init _ (self, name): self. name = names = Student ('bob') # method 1 bind the attribute s through the self variable of the class. score = 90 # method 2 direct assignment

3. Class binding method

The class binding method can be divided into two types. The first type is similar to the class binding attribute. The routine is as follows:

Class Student (object): passa = Student () # create instance def set_score (self, score): self. score = scoreStudent. set_score = set_score # class binding method. set_score (99) # Call method. score99 # output

The second method is to use MethodType to bind a class:

Class Student (object): passa = Student () # create instance def set_score (self, score): self. score = scorefrom types import MethodTypeStudent. set_score = MethodType (set_score, Student). set_score (99) # Call method. score99 # output

This method has a note, if you continue to create an instance B:

b=Student()b.set_score(60)b.scorea.score60

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

That is, instance a and B both reference this score as their own attributes. When it is modified, the corresponding attributes of all instances that reference it will change.

4. instance binding method

The first method is to bind a class to the instance, as shown in the preceding figure.

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

Class Student (object): passa = Student () # create instance def set_score (self, score): self. score = scorefrom types import MethodTypea. set_score = MethodType (set_score, a). set_score (99) # Call method. score99 # output

Note that this method only works for instance a. If you want to call all Studnet-class instances, bind the method directly to the class Student.

Summary

The above is all the content of this article. I hope the content of this article will help you in your study or work. If you have any questions, please leave a message, thank you for your support.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.