课程
/后端开发
/Python
/python进阶
class Person(object): def __init__(self, name, score): self.name = name self.__score = score p = Person('Bob', 59) print p.name print p.__score
2017-09-29
源自:python进阶 4-5
正在回答
私有属性外部无法访问,所以报错
邬玺邬玺
应为没有加try函数验证
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:
print "AttributeError"
报错提示:
Traceback (most recent call last): File "index.py", line 9, in <module> print p.__scoreAttributeError: 'Person' object has no attribute '__score'Bob
举报
学习函数式、模块和面向对象编程,掌握Python高级程序设计