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'
Bhargav Rao
52.6k29 gold badges130 silver badges142 bronze badges
2 Answers 2
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()
answered Jan 26, 2016 at 15:40
gtlambert
12k2 gold badges32 silver badges48 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
Harish
thank you very much , I feel so silly for this stupid mistake
_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()
1 Comment
Harish
thank you very much , I feel so silly for this stupid mistake
lang-py
def __init__(self).. Double underscore