【Python进阶】3-2 访问限制

我们可以给一个实例绑定很多属性,如果有些属性不希望被外部访问到怎么办?

Python对属性权限的控制是通过属性名来实现的,
如果一个属性由双下划线开头(__),该属性就无法被外部访问。
看例子:

class Person(object):
    def __init__(self, name):
        self.name = name
        self._title = 'Mr'
        self.__job = 'Student'
p = Person('Bob')
print p.name
# => Bob
print p._title
# => Mr
print p.__job
# => Error
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'Person' object has no attribute '__job'

可见,只有以双下划线开头的"__job"不能直接被外部访问。

但是,如果一个属性以"__xxx__“的形式定义,那它又可以被外部访问了,以”__xxx__“定义的属性在Python的类中被称为特殊属性,有很多预定义的特殊属性可以使用,通常我们不要把普通属性用”__xxx__"定义。

以单下划线开头的属性"_xxx"虽然也可以被外部访问,但是,按照习惯,他们不应该被外部访问。



任务:

请给Person类的__init__方法中添加name和score参数,并把score绑定到__score属性上,看看外部是否能访问到。

CODE:

class Person(object):
    def __init__(self, name, score):
        self.name = name
        self.__score = score

p = Person('Bob', 59)

print p.name
try :
    print p.__score
except AttributeError:
    print 'attributeerror'

在这里插入图片描述

发布了20 篇原创文章 · 获赞 0 · 访问量 391

猜你喜欢

转载自blog.csdn.net/yipyuenkay/article/details/104496639