Object-oriented -- binding and non-binding methods

Source: Internet
Author: User
The binding methods in the class are divided into two types: 1. Binding Methods

Special: the user who binds the API will call the API and the user who calls the API will automatically pass in the API as the first parameter.

Object binding method: If the function defined in the class does not have any modifier, it is bound to the object by default.

Method of binding to a class: Add a modifier classmethod to the class definition function and bind it to the class.

2. Non-binding method

Special: a non-binding method is a common function, that is, it is not bound to a class or an object, so that the class and object can be called, But no matter who calls it, it is a common function, no automatic Value Transfer Function

Three methods
Class FOO: # method of binding objects def func1 (Self): Print ("func1") # method of binding to classes @ classmethod def func2 (CLS): Print ("func ", CLs) # non-binding method @ staticmethod def func3 (x, y): Print (x + y)
Call Method

Method of binding an object. You can call an object or call a class, but you need to pass in the object.

F1 = Foo () # format of the bound object f1.func1 () Foo. func1 (F1)

Binding class

# Bind the class method Foo. func2 () f1.func2 ()

Non-binding method

# Non-binding method f1.func3 (1, 2) Foo. func3 (1, 2)

 

Object-oriented -- binding and non-binding methods

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.