Discuss / Python / self属性、类熟悉、实例属性理解

self属性、类熟悉、实例属性理解

Topic source
class Student(object):
    def __init__(self, name):
        self.name = name

s = Student('Bob')
s.score = 90

self作为所有属性的模板,不会因为实例s增加了score属性,在模板中增加参数score.

self属性与类属性同样作用于所有实例,区别在于:

self能通过参数来设计所有实例的属性模板;

类属性设定的是具体的属性,自带且固有的,可以通过实例属性进行覆盖。


  • 1

Reply