为了账号安全,请及时绑定邮箱和手机立即绑定

我只想打印出p1的结果,为什么p2能够自动运行输出?

#coding: utf-8
class Person(object):
    __count = 0
    def __init__(self,name):
        Person.__count = Person.__count  + 1
        self.name = name
        print Person.__count



p1 = Person('Bob')
p2 = Person('Alice')
try:
    print p1.__count
except :
    print 'AttributeError'

输出依然是:

1

2

AttributeError



正在回答

1 回答

1和2是你在运行代码p1 = Person('Bob') p2 = Person('Alice')时打印出来的,而非try expect函数打印出来的,因为—count是私有属性无法访问,所以打印AttributeError

2 回复 有任何疑惑可以回复我~
#1

慕仔8326853 提问者

谢谢大佬
2020-02-07 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消
python进阶
  • 参与学习       255772    人
  • 解答问题       2946    个

学习函数式、模块和面向对象编程,掌握Python高级程序设计

进入课程

我只想打印出p1的结果,为什么p2能够自动运行输出?

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信