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