1. Class attributes
Membership variables
Object creation
The process of creating an object is called instantiation. When an object is created, it contains three aspects of polypropylene attributes and methods.
Handles are used to distinguish different objects.
Object attributes and methods correspond to member variables and member functions in the class.
obj = MyClass() creates an instance of the class, a dilated object, and calls methods and properties through the object
Class attributes
Class attributes can be divided into public attributes and private attributes according to their scope of use, depending on the name of the attribute.
Common attributes - attributes that can be invoked both inside and outside
Private Attributes - Function calls outside and inside shellfish cannot be made
Definition: A member variable starting with a double underscore is a private property
It can be accessed by instance. class name attribute.
Built-in attributes -- consisting of double underscores, such as dic,module, added by default by the system when defining classes__
#!/usr/bin/env python #-*- coding:utf-8 -*- class People(object): color = 'yellow' __age = 30 #Private Attributes def think(self): self.color = "black" print "I am a %s " % self.color print ("I am a thinker") print self.__age ren = People() ren.color = 'White Man' print ren.color ren.think() print '#'*30 print("People.color") print ren.__People__age ##Used for testing. If you want to call it, call it through the method.
2. Class methods
Membership function
Class method
The definition of the method is the same as that of the function, but self is needed as the first parameter.
Class methods are:
public Method
Private methods
Class method
Static method
Common methods: Methods that are called in and out of classes are tested.
Private method: Unexpectedly, the external call module of the class, adding a "" c double underscore before the method is the private method.
self parameter:
Method for distinguishing functions and classes (there must be a self)
The self parameter represents the execution object itself
#!/usr/bin/env python #-*- coding:utf-8 -*- class People(object): color = 'yellow' __age = 30 #Private Attributes def think(self): self.color = "black" print "I am a %s " % self.color print ("I am a thinker") print self.__age def test(self): self.think() # Internal call jack = People() jack.test() #External call #!/usr/bin/env python #-*- coding:utf-8 -*- class People(object): color = 'yellow' __age = 30 #Private Attributes def think(self): self.color = "black" print "I am a %s " % self.color print ("I am a thinker") print self.__age def __talk(self): print "I am talking with Tom" def test(self): self.__talk() # Internal call talk() jack = People() jack.test() #External call
Class method
Class methods: Functions handled by classmethod() functions can be called by classes, and can also be called by objects (inheritance relations).
Static methods: equivalent to "global functions", can be called directly by classes, can be shared by all instantiated objects. Static methods () define static methods. Static methods have no self parameters.
Decorators: br/>@classmethod()
@staticmethod()
#!/usr/bin/env python #-*- coding:utf-8 -*- class People(object): color = 'yellow' __age = 30 #Private Attributes def think(self): self.color = "black" print "I am a %s " % self.color print ("I am a thinker") print self.__age def __talk(self): print "I am talking with Tom" def test(self): print 'Testing....' cm = classmethod(test) jack = People() People.cm() //Through the methods in the class method class, the attributes and methods that are not involved will not be loaded, which saves memory and is fast. #!/usr/bin/env python #-*- coding:utf-8 -*- class People(object): color = 'yellow' __age = 30 #Private Attributes def think(self): self.color = "black" print "I am a %s " % self.color print ("I am a thinker") print self.__age def __talk(self): print "I am talking with Tom" def test(): ##No self static call loads all properties into memory. print People.__age # Accessing internal variables through classes sm = staticmethod(test) jack = People() People.sm()
Decorate the method of calling the class:
#!/usr/bin/env python #-*- coding:utf-8 -*- class People(object): color = 'yellow' __age = 30 #Private Attributes def think(self): self.color = "black" print "I am a %s " % self.color print ("I am a thinker") print self.__age def __talk(self): print "I am talking with Tom" @classmethod #Calling methods of classes def test(self): print ("this is class method") @staticmethod #Calling methods of classes def test1(): print ("this is static method") jack = People() People.test() People.test1()