6. Referring to member variables


Tried the following code:

class Car(object):
    condition = "new"
    my_car = Car()

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

    my_car = Car("DeLorean", "silver", 88)

print my_car.condition
print my_car.model
print my_car.color
print my_car.mpg

And got this error message:

Traceback (most recent call last):
File "python", line 1, in
File "python", line 4, in Car
NameError: name 'Car' is not defined

Oops, try again. You need to pass three arguments when creating a Car().

What am I doing wrong? If I pass model, color, mpg into the Car() object on line 4 btw, I still get an error saying Oops, try again. It looks like you haven't created a Car object named my_car.


Hi @zsuzsifer ,

This is a good clue ...

NameError: name 'Car' is not defined

You need to check whether you have attempted to create an instance of the Car class before the definition of that class has been completed. Until it has been completed, the name, Car, is not yet defined.

A common source of such an error is one or more lines of code within a class definition that attempt to instantiate that class.


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