Python object-oriented programming binding method

Source: Internet
Author: User

The function properties of a class are bound to different objects and are different binding methods. When an object invokes a binding method, it passes the object itself as a self parameter
Sample code
classPeople:country=' China'? def __init__(self, name, sex, age): self. Name=name self. Sex=sex self. Age=Age ? defTest (self):Print(self)?? Conan= People ('Conan','male', 8) Wukong= People ('Wukong','male', 18)? Wukong.test ()#people.test (Wukong)Conan.test ()#people.test (Conan)?Print(People.test,people.test (123))Print(Wukong.test,wukong.test ())Print(Conan.test,wukong.test ())

Printing results:

# <__main__. People object at 0x10813a320>#  <__main__. People object at 0x10813a2e8>#  <function people.test at 0x1081291e0>#  <bound method People.test of <__main__. People object at 0x10813a320>>#  <bound method People.test of <__main__. People object at 0x10813a2e8>>

Python object-oriented programming binding method

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.