`#Python
class Car:
model=0
modelNumber=0
def _init_(self,model):
self.modelNumber=model
def getNum():
print modelNumber
obj = Car(5)
obj.getNum()
Could sombody tell me why I cannot give the constructor an argument
Traceback (most recent call last):
File "ex4.py", line 11, in
obj = Car(5)
TypeError: this constructor takes no arguments
It should be __init__
and not _init_
1 Like
Aside from what BaronOfCheese mentioned.. the getNum() method will always return 0.
If I had to guess what you were going for the class should look more like.
class Car:
def__init__
(self, model=0):
self.__modelNumber = model
def getNum(self):
return self.__modelNumber
I have no idea how to properly format indents on this site, but add the indents.
class Car:
def __init__(self, model=0):
self.__modelNumber = model
def getNum(self):
return self.__modelNumber
There fixed it for you
1 Like
It was supposed to I was just trying to get it to take an argument and my problem was I didnt include double underscores.