Understand class objects, instance objects, attributes, methods in Python

class Animal(object): # class object

    age = 0 # public class attribute
    __like = None # private class attribute

    def __init__(self): # magic method
        self.name = 'haha' # public instance attribute
        self.__sex = 'man' # private instance attribute

    def smile(self): # The public method self points to the instance object
        pass

    def __jump(self): # private method
        pass

    @classmethod
    def run(cls): # The class method cls points to the class object
        pass

    @staticmethod
    def msg(): # static method, can have no parameters
        pass

'''
# class object:
By abstracting objects with similar properties and methods into class objects, some similar properties and methods can be defined, and different instance objects can refer to the properties and methods of class objects, which can reduce the repetition rate of code.

# Class properties:
All attributes of class objects, class objects and instance objects can be accessed and shared by them;

# Public class properties:
Class attributes can be modified outside the class, and need to be modified directly through the class object reference;
Class properties can be modified within a class through class methods.
If you refer to a class attribute through an instance object, it is equivalent to the instance object creating a name with the same name as the class attribute in the instance method, which is equivalent to the local variable instance attribute and has nothing to do with the class attribute;

# Private class properties:
Outside the class, the class object reference cannot be changed directly, and the class object can only be changed by calling the instance method.

# class method:
The decorator @classmethod is required to indicate that it is a class method. The first parameter of the class method must be a class object, which is generally represented by cls, and the class attributes and class methods referenced through cls must be.

# Static method:
The decorator @staticmethod is required to mark it as a static method. Some functions need to be done, which are related to classes, but do not require the participation of classes and objects. In this case, static methods can be used, and static methods can require no parameters.

# Instance object:
Instance objects created from class objects

# Instance properties:
properties defined by methods

# Private instance properties:
A variable name defined at the beginning of __; can only be changed by a method call
Public instance properties:
Can be redefined by instance object

# Instance method:
The first parameter is recognized as self, which can call class objects and instance objects.

# Private method:
It can only be called within the class. If it is called outside the class, the public method within the class needs to be called to the private method within the class. If the public method is called outside the class, the private method is indirectly called.
The core content of a program cannot be called at will. It can be set as a private method, and the private method can be called through the judgment of the public method!

# Public methods:
It can be called freely inside and outside the class.

# Magic method:
For example, the __init__ method, with two methods wrapped around __
'''

  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325949192&siteId=291194637