Methods for finding and binding python-properties

Source: Internet
Author: User

Properties of the class:

There are two types of properties in a class:

Data properties: The data properties of a class are shared by all objects.

  Eg: a school = ' luffycity ' #这个称类的数据属性 is defined in the class.

When this class generates multiple objects, these objects all enjoy this data property.

   Function Properties: A function property of a class is bound to the object used.

     Eg: a function is defined in the class:

Def eat ():

Print (' is eatting ')

When a Stu1 object is generated and the Eat method is used, the execution results affect only STU1

   Note: When an object is defined, it looks in the following order:

The property is first searched for by the object's namespace, and when it is not found,

will be found from the namespace of the class, and can not find, in the parent class namespace to find, or cannot find, will be an error.

Binding method:

The function defined in the class--is the function property of the class, the class can use itself, but must obey the function's parameter rules, there are several parameters that need to pass several parameters.

classOldboystudent:school='Oldboy'    def __init__(self,name,age,sex): Self.name=name Self.age=Age Self.sex=SexdefLearn (self):Print('%s is learning'%self.name)#New Self.name    defEat (self):Print('%s is eating'%self.name)defSleep (self):Print('%s is sleeping'%self.name) S1=oldboystudent ('Lee Tank','male', 18)Print(Oldboystudent.learn (S1))#the Learn class uses the function defined by the class to pass the argument.                                #now, S1 the object in.                               #can be
View Code

Note: 1. The functions defined in the class are primarily used by objects, and are bound to objects.

2, the function of the class defines the same function, but the class binds to the different object, is the different binding method.

3, the binding object of the method of this automatic value characteristics, determines the class defined in the function is to write a self parameter.

Class is type:

Everything in Python3 is object, and class and type in Python3 is the same concept, and type is class.

Methods for finding and binding python-properties

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.