属性错误对象没有属性

时间:2016-01-26 15:39:12

标签: python

我只是想尝试一些课程,然后我陷入Basics ..我的代码如下:

class Prob():
    def _init_(self):
        self._count = 0
    def _ProbCal(self):
        print(self._count)
d = Prob()
d._ProbCal()

错误是:

Traceback (most recent call last):
  File "ProbCalculation.py", line 8, in <module>
    d._ProbCal()
  File "ProbCalculation.py", line 6, in _ProbCal
    print(self._count)
AttributeError: 'Prob' object has no attribute '_count'

2 个答案:

答案 0 :(得分:1)

您的__init__函数在方法名称的开头和结尾需要双下划线:

class Prob():
    def __init__(self):
        self._count = 0

    def _ProbCal(self):
        print(self._count)

d = Prob()
d._ProbCal()

答案 1 :(得分:0)

_init_必须有双重下划线,如下所示:

class Prob():
    def __init__(self):
        self._count = 0
    def _ProbCal(self):
        print(self._count)
d = Prob()
d._ProbCal()