类属性和实例属性
类属性和实例属性
class Farther(object):
name = 'Farther.name' # 类属性,类的所有实例均可访问,包括字类
def __init__(self):
self.name = 'Farther.self.name' # 实例属性
class Son(Farther):
def __init__(self):
self.name = 'son.self.name'
son = Son()
far = Farther()
print(son.name) # son.self.name
print(Son.name) # Farther.name
print(far.name) # Farther.self.name
print(Farther.name) # Farther.name
同名属性,对于类的属性来说先类本身(实例)的属性,再寻找类属性,之后在父类的类属性中寻找
ps:程序中,实例属性与类属性最好不要重名

浙公网安备 33010602011771号