Initializing a class


#1


Traceback (most recent call last):
File "python", line 8, in
File "python", line 4, in init
NameError: global name 'condition' is not defined

I dont understand the error, in codeacedemy it has been accepted.

Replace this line with your code. 
class Car(object):
    condition="new"
    def __init__(self,model,color,mpg):
        self.model=condition
        self.color=self.condition
        self.mpg=self.condition

my_car=Car("DeLorean","silver",88)
print my_car.condition


#2

MY doubt is that do we need the dot notation to access our member variables which are created in the class itself also? since in
"Referring to member variables"
it has been written that ;-
" Calling class member variables works the same whether those values are created within the class (like our car's condition) or values are passed into the new object at initialization. We use dot notation to access the member variables of classes since those variables belong to the object "


#3

since there is no condition parameter, the error is being raised. You have three parameters and three corresponding member variables, Should you not be assigning the parameters accordingly?


#4

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