python class instance appreciated recorded

Method class python

class Person(object):
    # 类属性
    name = "xiaohong"
    #name_list = ["xiaohong","xiaoming"]

    # 实例方法
    def set_age(self):
        # 类属性 复制一份数据到 实例中
        print(self.name is Person.name,"1")
        # 重新赋值后可以看到 类属性和实例属性 并不是同一个东西,只是值相同,名称相同
        # 实例属性
        self.name = "xh"
        print(self.name is Person.name,self.name,Person.name,"2")
        self.score = 12
    #类 方法
    @classmethod
    def get_value(cls):
        #类方法 访问的是 类属性 不是复制
        print(cls.name)
        cls.name = "xj"
        print(cls.name,Person.name)
    @classmethod
    def get_score(cls):
        print(cls.score)

Person().set_age()
Person.get_value()
print(Person.name) # 访问类属性
print(Person().name) #访问实例属性 实例属性没有初始化但是有值,是类属性复制了一份到 实例中
try:
    print(Person.age)
except Exception as e:
    print(e.args)
Person.age = 12 #给 类 添加属性
print(Person.age)

print("######测试 类方法 不能访问 实例属性")
A = Person()
print(A.set_age())
A.get_score()

===================
Traceback (most recent call last):
  File "D:/pycharm_project/helloworld/test_unittest/test02.py", line 40, in <module>
True 1
False xh xiaohong 2
xiaohong
xj xj
xj
xj
("type object 'Person' has no attribute 'age'",)
12
######测试 类方法 不能访问 实例属性
True 1
False xh xj 2
None
    A.get_score()
  File "D:/pycharm_project/helloworld/test_unittest/test02.py", line 24, in get_score
    print(cls.score)
AttributeError: type object 'Person' has no attribute 'score'

noun:

  • Examples of methods 无装饰器
  • Instance Properties
  • Class Methods @classmethod
  • Class Properties
  • Static method @staticmethod

Class Properties

  • Values can directly access an instance method of class attribute
    (because the class property instance to copy data, unless the attributes of the corresponding instance reassigning example to access to the data)
  • Class attributes can not be directly modified
  • Or directly access and reassigned by class instance methods are possiblePerson.name

Examples of attributes:

  • Examples of direct access and modify the properties in the example method
  • In the class or method can not directly access the modified instance attribute
  • But to access or modify the attribute instance by accessing the instance method

Examples of methods and class methods:

  • Examples of methods for accessing the class methods
  • Examples of the method class methods can

Static method

  • Instance methods and class methods can access static methods
  • Static method, can not directly access the class attribute, attribute instance, the class and instance methods can not be directly accessed (to be accessed indirectly via first class or instance that is created)
  • This method is not as defined in the classes, methods and classes outside the only difference, you need to access to the instance or class method
  • Direct static method does not directly access, independently of one another, also need to be accessed using the indirect instance or class

Guess you like

Origin www.cnblogs.com/wanderingfish/p/12116368.html