python's class attributes, instance attributes, class methods, static methods

Posted by Renegade85 on Tue, 20 Aug 2019 10:58:22 +0200

Class attributes

Like the following code:

class Person:
    name = "Zhang San" # Common class attributes
    __age = 18 # Private class attributes

The attributes directly defined in a class are class attributes, which are shared by all instance objects.
For common class attributes, class objects and instance objects can be accessed outside the class.
For example:

class Person:
    name = Zhang San # Common Class Attributes
    _ age = 18 # private class attribute

p = Person()
print(p.name)  Accessing common properties through instance objects
 print(Person.name)  Accessing common properties through class objects
"""
Output results:
Zhang San
 Zhang San
"""

Private class attributes cannot be accessed outside the class, otherwise exceptions will be reported.

Instance attributes

  • The attributes defined in a class by self.xxx or instance object. xxx are instance attributes.
  • Each instance attribute only exists in the current instance object. If you want all instance objects to have the instance attribute, you need to define the instance attribute in the _init_ initialization method.

Define instance attributes as shown in the following code:

class Person:
    def __init__(self):
        self.name = "Zhang San" # Define instance attributes
    

p = Person()
print(p.name)

perhaps

class Person:
    pass

p = Person()
p.name = "Zhang San" # Define instance attributes
print(p.name)

Modifying the values of class attributes through class objects

Perhaps we would think of modifying the value of class attributes through instance objects, so it's like this:

class Person:
    sex = "male"
    def __init__(self):
        self.sex = "female"

p = Person()
print(p.sex)
print(Person.sex)
"""
//Output results:
//female
//male
"""

It may also be that:

class Person:
    sex = "male"

p = Person()
p.sex = "female"
print(p.sex)
print(Person.sex)
"""
//Output results:
//female
//male
"""

Essentially, they are the same. They do not modify the value of class attributes, but add an instance attribute with the same name as class attributes to the current instance object. If the attribute of that name is referenced through the instance object, the instance attributes will be forcibly shielded from class attributes, that is, the instance attributes are referenced unless deleted. This instance property.
Only through class objects can the values of class attributes be modified

class Person:
    sex = male

Person.sex = female
print(Person.sex)
"""
Output results:
female
"""

Class method

Class method is the method that class object possesses. It needs to be identified as class method by modifier @classmethod. For class method, the first parameter must be class object, and CLS is usually used as the first parameter (of course, variables with other names can be used as its first parameter, but most people are accustomed to using'cls'as the first parameter). The name of each parameter is best used in'cls', which can be accessed through instance objects and class objects.

class People(object):
    country = 'china'

    #Class methods, modified with class methods
    @classmethod
    def getCountry(cls):
        return cls.country

p = People()
print(p.getCountry())    #You can use instance object references
print(People.getCountry())    #References can be made through class objects
"""
//Output results:
china
china
"""

Another function of class methods is that they can modify class attributes:

class People(object):
    country = 'china'

    #Class methods, modified with class methods
    @classmethod
    def getCountry(cls):
        return cls.country

    @classmethod
    def setCountry(cls,country):
        cls.country = country


p = People()
print(p.getCountry())    #You can use instance object references
print(People.getCountry())    #References can be made through class objects

p.setCountry('English')   

print(p.getCountry())    
print(People.getCountry())
"""
//Output results:
china
china
English
English
"""

Static method

If the parameters passed by a method are not related to instance attributes, then it can be defined as a static method, which needs to be modified by the modifier @static method. A static method does not need to define more parameters.

class People(object):
    country = 'china'

    @staticmethod
    def getCountry():
        return People.country


print (People.getCountry())

summary

From the definition of class method, instance method and static method, we can see that the first parameter of class method is class object cls, then the reference through CLS must be the attribute and method of class object; while the first parameter of instance method is instance object self, then the reference through self may be class attribute or possible. It can be instance attributes (which need to be analyzed in detail), but in the case of class attributes and instance attributes with the same name, instance attributes have a higher priority. There is no need to define additional parameters in static methods, so if class attributes are referenced in static methods, they must be referenced through class objects.

Topics: Python Attribute