Python Class() takes no argument error. I am using Self
Question:
I’ve been going through both Stack and the internet at large but I’m not able to find the issue I’m dealing with. I’m new to Python and I’m learning how classes work. I’ve written up a very simple class and function to print it to the console.
class Car():
def __intit__(self, make, model, year):
self.make = make
self.model = model
self.year = year
def get_descriptive_name(self):
long_name = str(self.year), + " " + self.make + " " + self.model
return long_name.title()
my_new_car = Car('Chevy', 'sonic', 2015)
print(my_new_car.get_descriptive_name())
But when I run the compiler it returns the error Car() takes no arguments. I’m aware of the need for self parameter and I’ve included them in all the areas I can think of putting one. I’ve done a lot of experimentation on my end and nothing I try changes it. I was hoping someone could explain this to me or might know of a relevant thread?
Thanks in advance for any help!
Answers:
Simple typo:
def __intit__(self, make, model, year):
should be __init__
.
You have no __init__
method that takes the instance arguments as you have mispelled it for def __intit__(self, make, model, year):
. As a result, when instantiating my_new_car = Car('Chevy', 'sonic', 2015)
, you are passing arguments to Car that does not expect them
class Car():
def __init__(self, make, model, year):
self.make = make
self.model = model
self.year = year
def get_descriptive_name(self):
long_name = str(self.year) + " " + self.make + " " + self.model
return long_name.title()
my_new_car = Car('Chevy', 'sonic', 2015)
print(my_new_car.get_descriptive_name())
You have two errors,
def __init__
(like above)
long_name = str(self.year) + " " + self.make + " " + self.model
after str(self.year)
you have unnecessary commas
I’ve been going through both Stack and the internet at large but I’m not able to find the issue I’m dealing with. I’m new to Python and I’m learning how classes work. I’ve written up a very simple class and function to print it to the console.
class Car():
def __intit__(self, make, model, year):
self.make = make
self.model = model
self.year = year
def get_descriptive_name(self):
long_name = str(self.year), + " " + self.make + " " + self.model
return long_name.title()
my_new_car = Car('Chevy', 'sonic', 2015)
print(my_new_car.get_descriptive_name())
But when I run the compiler it returns the error Car() takes no arguments. I’m aware of the need for self parameter and I’ve included them in all the areas I can think of putting one. I’ve done a lot of experimentation on my end and nothing I try changes it. I was hoping someone could explain this to me or might know of a relevant thread?
Thanks in advance for any help!
Simple typo:
def __intit__(self, make, model, year):
should be __init__
.
You have no __init__
method that takes the instance arguments as you have mispelled it for def __intit__(self, make, model, year):
. As a result, when instantiating my_new_car = Car('Chevy', 'sonic', 2015)
, you are passing arguments to Car that does not expect them
class Car():
def __init__(self, make, model, year):
self.make = make
self.model = model
self.year = year
def get_descriptive_name(self):
long_name = str(self.year) + " " + self.make + " " + self.model
return long_name.title()
my_new_car = Car('Chevy', 'sonic', 2015)
print(my_new_car.get_descriptive_name())
You have two errors,
def __init__
(like above)long_name = str(self.year) + " " + self.make + " " + self.model
afterstr(self.year)
you have unnecessary commas