class Parent:
def _init_(self,x):
self.x=x
def count(self,x):
self.x = self.x + 1
class Child(Parent):
def _init_(self, y=0):
Parent._init_(self,3)
self.y = y
def count(self):
self.y +=1
obj=Child()
obj.count()
print(obj.x,obj.y)
I would like to see the output of print(obj.x,obj.y). However, I keep getting this error:
Traceback (most recent call last):
File “./code_with_class.py”, line 19, in
obj.count()
File “./code_with_class.py”, line 16, in count
self.y +=1
AttributeError: ‘Child’ object has no attribute ‘y’
What do I need to do to correct this? How do I improve this code?
Python is very well documented. In the tutorial part, you will find this here:
“Of course, the __init__() method may have arguments for greater flexibility. In that case, arguments given to the class instantiation operator are passed on to __init__().”