Initializing a class


#1



https://www.codecademy.com/en/courses/python-intermediate-en-egNXj/0/5?curriculum_id=4f89dab3d788890003000096#


Traceback (most recent call last):
File "python", line 3, in
TypeError: object.new() takes no parameters


I'm not sure what it is I am doing incorrectly.


lass Car(object):
    condition = 'new'
my_car = Car("DeLorean","silver",88)


def __init__(self, model, color, mpg):
    
    self.model = model
    self.color = color
    self.mpg = mpg
    

print my_car.condition


#2

init is a method, it should be part of your class content


#3

I see. So I should indent init and everything within it?


#4

And it should be at the right place. First the class with member variables (condition) and methods (init), once you finished building your class, you can create a class instance (my_car)


#5

You should:
1. Create a class(this case: class Car(object):)
2. Define the member variables of your class(this case: condition = "new")
3. Define the special funcion and its own parameters(this case:
def __ init __(self, model, color, mpg):
self.model = model
self.color = color
self.mpg = mpg
4. After that, you can create your own objects and call the member variables and parameters of the class you've created. (my_car = Car("DeLorean", "silver", 88))


#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.