我们可以给一个实例绑定很多属性,如果有些属性不希望被外部访问到怎么办?
Python对属性权限的控制是通过属性名来实现的,如果一个属性由双下划线开头(__),该属性就无法被外部访问,
Python访问限制
。看例子:class Person(object): def __init__(self, name): self.name = name self._title = 'Mr' self.__job = 'Student'p = Person('Bob')print p.name# => Bobprint p._title# => Mrprint p.__job# => ErrorTraceback (most recent call last): File "<stdin>", line 1, in<module>AttributeError: 'Person' object has no attribute '__job'</module></stdin>可见,只有以双下划线开头的"__job"不能直接被外部访问,
电脑资料
《Python访问限制》(https://www.unjs.com)。但是,如果一个属性以"__xxx__"的形式定义,那它又可以被外部访问了,以"__xxx__"定义的属性在Python的类中被称为特殊属性,有很多预定义的特殊属性可以使用,通常我们不要把普通属性用"__xxx__"定义。
以单下划线开头的属性"_xxx"虽然也可以被外部访问,但是,按照习惯,他们不应该被外部访问。
任务
请给Person类的__init__方法中添加name和score参数,并把score绑定到__score属性上,看看外部是否能访问到。
- ?不会了怎么办
以双下划线开头的属性无法被外部访问,"__xxx__"除外。
参考代码:
class Person(object): def __init__(self, name, score): self.name = name self.__score = scorep = Person('Bob', 59)print p.nameprint p.__score