Reputation: 19
I just wanted to try some classes then I am stuck in Basics ..My code is below :
class Prob():
def _init_(self):
self._count = 0
def _ProbCal(self):
print(self._count)
d = Prob()
d._ProbCal()
error is :
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'
Upvotes: 0
Views: 64
Reputation: 332
_init_
Must have double underscores like so:
class Prob():
def __init__(self):
self._count = 0
def _ProbCal(self):
print(self._count)
d = Prob()
d._ProbCal()
Upvotes: 0
Reputation: 11961
Your __init__
function requires double underscores at the start and end of the method name:
class Prob():
def __init__(self):
self._count = 0
def _ProbCal(self):
print(self._count)
d = Prob()
d._ProbCal()
Upvotes: 1