我只是想尝试一些课程,然后我陷入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'
答案 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()