Dynamic binding of Method properties and method properties of classes in Python

Source: Internet
Author: User
Tags abstract language instance method

Recently in the study of Python, purely their own interests, but did not systematically read the Python programming books, feel that the above description is too cumbersome, in the site to find some learning site, found Liaoche Teacher's website above the learning resources is very good, and concise, Extracts some of the important syntax and cases in Python. It is important to test Python's running code online, the drawback is that there is no systematic look at the Python books, can not be timely to the fragmentation of knowledge linked together, this is the difference between reading and not reading. Especially in the Python class and instance of the method of the call to feel foggy, after thinking about their own ideas recorded, one to deepen their understanding, to consolidate their memories, and to help some want to learn Python friends understand this abstract language, understand the wrong place, I hope you give correct, thank you.

1. Classes and instances in Python

First define a class

class Student (object):

initialization, binding some of the necessary properties to the student class

def __init__ (self, Name, score):         = name        = Score

Internally define a function that implements property operations on incoming instances, encapsulates the data internally, and encapsulates the data itself as a method of associating a class, called a class.

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

2. Invocation of properties and methods owned by the instance

Pass in an instance

Bart = Student ('Bart Simpson', 59)

A call to a property

>>> bart.name'Bart Simpson'

Call to Method

>>>59

No problem to understand the binding of properties and methods in the instance of Python

3. Instance properties and method bindings in Python

First define a class

class Student (object):     Pass

Pass in an instance

s = Student ()

Dynamically binding an attribute to an instance

' Michael '

Next, bind the instance to the method

Define a function first

def # define a function as an instance method    Self.age = Age

Method binding on an instance

 from Import Methodtype # binding a method to an instance # Invoking instance methods # Test Results 25

Method binding to a class

def Set_score (self, score):# defines a function as a method of a class     Self.score =set_score # Binding Method

Teacher Liao added in the following sentence: The set_score method can be defined directly in class, but dynamic binding allows us to dynamically add functionality to class while the program is running, which is difficult to implement in a static language.

I understand that the general definition of a class, the class will be initialized at the time of the binding of the property, the incoming instance directly into the instance with parameters, through the internal definition of a number of methods, the instance can directly inherit the properties and instances of the method of the class data manipulation, reference, such as Xxx.name The form of Xxx.print_name. However, if the defined class is not initialized, based on the dynamic binding properties of the Python language, we can bind the instance and method to the incoming instance, the binding of the property is simple, the binding of the method needs to be through the form of the From types import Methodtype, (Other forms are not known for the time being), telling the interpreter that S.set_age's method operation is to bind the Set_age function to s, s.set_age = Methodtype (Set_age, s), so Python knows how to execute the Set_age method of S. However, this binding method can only work on a bound method instance in a class, and it is necessary to dynamically bind a method to the class in order to take effect on all instances of the class. Just like we saw above. After binding, the call can then be made directly in the form of a xxx.name xxx.print_name.

Whether a method can be called directly, whether a defined function is defined in a class or a function-based definition , for a function that is not defined in a class, the implementation of a method operation on an instance requires a dynamic binding, or a method binding on the class to which the instance belongs Whereas a function defined in a class is a method that can be called directly in an instance.

Dynamic binding of Method properties and method properties of classes in Python

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.