python class attributes, attribute instance, class methods, static methods

Class Properties

Like the following code:

class Person:
	name = "张三" # 共有类属性
	__age = 18 # 私有类属性

Directly defined in the class attribute is the class attribute, which is common to all instances of objects .
For total class attributes, and can be accessed through the class object instance of the object outside the class .
E.g:

class Person:
	name = "张三" # 共有类属性
	__age = 18 # 私有类属性

p = Person()
print(p.name) # 通过实例对象访问共有属性
print(Person.name) # 通过类对象访问共有属性
"""
输出结果:
张三
张三
"""

Private class attributes can not be accessed outside the class, otherwise it will report an exception.

Instance Properties

  • Or instance of an object by self.xxx .xxx defined properties is in the class instance attributes.
  • Each instance of the current instance of the object property is only there, if you want to have all the instance object instance attribute, define the instance attribute __init __ initialization process.

Examples of attributes shown in the code are defined as follows:

class Person:
	def __init__(self):
		self.name = "张三" # 定义实例属性
	

p = Person()
print(p.name)

or

class Person:
	pass

p = Person()
p.name = "张三" # 定义实例属性
print(p.name)

By modifying the value of the class object class attribute

Maybe we'll think to change the value of the property through a class instance object, then it is this:

class Person:
	sex = "男"
	def __init__(self):
		self.sex = "女"

p = Person()
print(p.sex)
print(Person.sex)
"""
输出结果:
女
男
"""

You probably think:

class Person:
	sex = "男"

p = Person()
p.sex = "女"
print(p.sex)
print(Person.sex)
"""
输出结果:
女
男
"""

Essentially they are the same, and does not modify the value of the class attribute, just add a and class attributes with the same name instance attributes in the current instance of the object, and then if for referencing the attribute of the name by way of example the object instance attributes will force the shield out of class attributes that reference the instance property, unless the property instance to delete.
Only class object can modify the value of the property by class

class Person:
	sex = "男"

Person.sex = "女"
print(Person.sex)
"""
输出结果:
女
"""

Class Methods

The method has a class object is a class method, decorator @classmethod need to identify which type of process for a class method, the first parameter must be a class object, generally cls as the first parameter (of course, other names may be used variable as its first argument, but most people are used to the 'cls' as the name of the first argument, it is best to use 'cls' a), able to access through the instance object and class objects.

class People(object):
    country = 'china'

    #类方法,用classmethod来进行修饰
    @classmethod
    def getCountry(cls):
        return cls.country

p = People()
print(p.getCountry())    #可以用过实例对象引用
print(People.getCountry())    #可以通过类对象引用
"""
输出结果:
china
china
"""

Class method there is a role that can modify class properties:

class People(object):
    country = 'china'

    #类方法,用classmethod来进行修饰
    @classmethod
    def getCountry(cls):
        return cls.country

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


p = People()
print(p.getCountry())    #可以用过实例对象引用
print(People.getCountry())    #可以通过类对象引用

p.setCountry('English')   

print(p.getCountry())    
print(People.getCountry())
"""
输出结果:
china
china
English
English
"""

Static method

If a parameter is not passed and the method of the instance attribute related, then it could be defined as static methods, need to be modified by modifying an @staticmethod, the static method does not require a multi-defined parameters.

class People(object):
    country = 'china'

    @staticmethod
    def getCountry():
        return People.country


print (People.getCountry())

to sum up

From the class and instance methods and the definition of static methods can be seen, the first parameter is a class object class method cls, it must be referenced by cls properties and methods of an object class; the first example of a method parameter is an instance of an object self, then by self reference may be class attributes, there could be an instance attribute (this requires specific analysis), but in the presence of class and instance properties of the same name, a higher instance attributes priority. No additional static method defined parameters, reference properties in a static method, it must be referenced by the class object.

Published 44 original articles · won praise 8 · views 2471

Guess you like

Origin blog.csdn.net/qq_39659278/article/details/99847339