6. Referring to member variables need help please


#1



i've tried all kinds of ways, one that doesn't seem to work is,, my_car = Car() then it asks for 3 variables???

need help please, thanks

6. Referring to member variables

Oops, try again. my_car does not appear to be an instance of Car.

i expect to get the correct code to pass..


class Car(object):  

    my_car.model="Delorean" 
    my_car.color="silver" 
    my_car.mpg=88 

condition = "new"

def __init__(self, model, color, mpg):
   
    self.model = model   
    self.color = color
    self.mpg = mpg
    
#mycar = Car(self, model, color, mpg)
      
print my_car.condition
print my_car.model
print my_car.color
print my_car.mpg


#2

These are instance variables that belong to a class instance. They should not be inside the class definition.

class ...
    condition = "new"
    def __init__( ... )
        self.model = ...
        # ...

my_car = Car( ... )

The arguments will be the data used in your instance variables.


#3

really a appreciate your helping me MTF, i'm struggling with this material, i google the problem, after some hours if i can't figure it out then i go to the forum,

i moved the instance variables to the class area,

then i entered this code

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

then i got this error..

Oops, try again. Make sure you pass the self keyword to the init() function.

now what please?????


#4

i put the exercise into a loop, i'm shutting down my computer, i can't restart the exercise??


#5

Be sure your __init__ method is indented, written as it is in the opening post.

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

#6

i'm tired, been working on this for 5 hours now??

so far i have this:

class Car(object):
        condition = "new"
def init(self, model, color, mpg):
    self.model = model
    self.color = color
    self.mpg = mpg

class Car(object):
    def __init__(self, model, color, mpg):
            my_car = Car("DeLorean", "silver", 88)


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

get this:
Oops, try again. my_car does not appear to be an instance of Car. i am trying to figure out you suggestions not getting to far, i'll work on it tomorrow when i get back from my part time job, thanks again for helping me.


#7

class Classname(object):
    class_var = "default value"
    def __init__(self, member1, member2, member3):
        self.member1 = member1
        self.member2 = member2
        self.member3 = member3

That is an example of a class definition with an __init__ method. Instances are declared in global scope.

my_class_instance = Classname("one", "two", "three")

Now we have an instance and may query its variables with dot notation.

print my_class_instance.class_var    # default value
print my_class_instance.member1      # one
print my_class_instance.member2      # two
print my_class_instance.member3      # three

#8

hi mtf,

not working toooo wellll?

i really appreciate you sending me the class definition visual description, i tried to do as the example showed, but, alas, i failed, i kinda sorta get the class def model, still can't figure it out.

model = ""
color = ""
mpg = 0
class Car(object):
Car = (model, color, mpg)
my_car = Car

def __init__():   

    car.model ="delorean"
    car.color = "silver"
    car.mpg = 88


    condition = "new"

print condition
print Car.model
print Car.color
print Car.mpg

thanks mtf,

jhg


#9

Okay, let's take it from the top. First, toss out the three lines,

model = ""
color = ""
mpg = 0

They are not needed.

Start with a class header, making your class inherit from object...

class Car(object):

Now indent, and fill in the class variable(s):

    condition = "good"

Notice that class variables are not written inside the init method.

Next, at the same level of indent, write the __init__ method with self, and the other three parameters.

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

Now at another level of indent, write the three instance variables:

        self.model = model
        self.color = color
        self.mpg = mpg

Those three lines will assign the init parameter values to the instance variables.

That's the complete class definition. Now with no indentation, (in global scope) we create an instance of the class, passing in the three arguments in the order in which the init method expects them.

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

At this point, we may now query or even modify the variables.

Aside

What is self? Loosely described, it is an object of the class that starts out empty, but then gets populated with the members created, both class and instance variables. It belongs to class so we must past it in to the __init__ method so it can be accessed. Note that condition is defined directly on the class, not on the instance. That is why it has no self prefix.

In simple terms, self is often referred to as the, context object since each instance will have its own context. Context is sort of like closure around each instance that sets one instance apart from another. my_car is one context, your_car (assuming it is created) would be another.

When accessing instance or class variables, we must always name the context,

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

#10

Hi mtf,

i got it to work: big thanks to you..

class Car(object):
condition = "new"

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

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

my_car.model="DeLorean"
my_car.color="silver"
my_car.mpg=88

print condition

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

one main problem i had was the '''Delorean'', which is incorrect, per the error stated, after looking at this error closely, the correct way that was wanted was ''''DeLorean''''' the stupid capital ''L'' did me in.. anyway,

fixed it to ''L'' it worked.

thanks

jack g


#11

Yeah, I made the same mistake. Glad you found your way through it. Keep an eye on the little things... Especially limit the number of details to exactly what the exercise expects. Expect these lessons to be simplistic, so if things are getting complicated, it's a sign to start anew.


#12

mtf, i really appreciate your time in helping me, i'd like to send you a
starbucks card to say thanks, where would i send the card?

thanks again,

jack g


#13

Appreciate the thought, but there are no starbucks in my area. You could just pay it forward and buy the coffee for the person behind you in line.


#14

ok, i'll do that, have a nice night


#15

hi mtf,, btw,, how did you get so good at this python stuff??


#16

Good is not the word I would use. I am still way behind the curve. It's just fun to learn, and for me it's only a hobby. My main focus is on little things, still very much basics. Solving small problems is best for me, picking up little details along the way.


#17

i sure appreciate your helping me


#18

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