Help on 9/11


#1

I need your help on 9/11. My code is following, I do not know why it returns "my car does not have a model member variable." Could you please help me out what to do here? Than you so much!

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

def display_car(self):
    return "This is a " + str(self.color) +" "+ str(self.model) + " with " + str(self.mpg) + " MPG."

def drive_car(self):
    self.condition = "used"

class ElectricCar(Car):
def init(self, battery_type):
self.battery_type = battery_type

my_car = ElectricCar("molten salt")

print my_car.condition
my_car.drive_car()
print my_car.condition


#2

If you want to inherit from another class you have to assign it in the new class. Especially if it is contained in the init function.

Example:

class One(object):
    def __init__(self, name):
        self.name=name

class Two(One):
    def __init__(self, name, last_name):
        One.__init__(self, name)
        self.last_name = last_name

my_self = Two('Jeb', 'Able')

EDIT:

@hyon0930

If I was not clear enough that means you have to put the init function call as I do in my second class. Then you have to pass the variables your first class's init function needs. Or you will encounter errors.


#3

This is my code:

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

def display_car(self):
    print "This is a %s %s with %s MPG." % (self.color, self.model, str(self.mpg))

def drive_car(self):
    self.condition = "used"
    return self.condition

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

print my_car.condition
print my_car.drive_car()

class ElectricCar(Car):
def init(self, battery_type):
Car.init(self, model, color, mpg)
self.battery_type = battery_type

my_car = ElectricCar("Chevrolett", "black", 0, "molten salt")

print my_car.battery_type

This is my error message:

Traceback (most recent call last):
File "python", line 25, in
TypeError: init() takes exactly 2 arguments (5 given)
Oops, try again.
my_car does not appear to be an instance of ElectricCar.

Any idea what I'm doing wrong? Please help! Thank you.


#4

@blackbat023: I don't know if you read @zeziba's post but he gives a good exemple on how you need to refer to the parent class from within the child's __init__ definition.

Also, whenever you would like to paste code and have it remain its format and indentation, since this forum supports Markdown, you can use three backticks before and after your code block like so:

```
your code
```

and it will render in an easy to read fashion:

class My_PlaneTicket(object):
    def __init__(self, seat_num):
        self.seat_num = seat_num

class My_EconomyClass(My_PlaneTicket):
    def __init__(self, seat_num, snack):
        My_PlaneTicket.__init__(self, seat_num)
        self.snack = snack

class My_BusinessClass(My_EconomyClass):
    def __init__(self, seat_num, snack, pillow):
        My_EconomyClass.__init__(self, seat_num, snack)
        self.pillow = pillow

class My_FirstClass(My_BusinessClass):
    def __init__(self, seat_num, snack, pillow, meal, wine):
        My_BusinessClass.__init__(self, seat_num, snack, pillow)
        self.meal = meal
        self.wine = wine

#5

This was successful for me:

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

def display_car(self):
    return "This is a %s %s with %s MPG."%(self.color, self.model, str(self.mpg))

def drive_car(self):
    self.condition = "used"

class ElectricCar(Car):
def init(self, model, color, mpg, battery_type):
self.model = model
self.color = color
self.mpg = mpg
self.battery_type = battery_type

my_car = ElectricCar("DeLorean", "silver", 88, "molten salt")


#6

@jmok81: In this particular exemple it does work but my understanding is that your code is overriding the parent's arguments instead of inheriting them.

The whole point of class inheritence is having changes made to your parent classes reflected into your child classes... :smile:


#7

Thanks @denisaltroy . Had another look at it and came up with this:

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

def display_car(self):
    return "This is a %s %s with %s MPG."%(self.color, self.model, str(self.mpg))

def drive_car(self):
    self.condition = "used"

class ElectricCar(Car):
def init(self, model, color, mpg, battery_type):
Car.init(self, model, color, mpg)
self.battery_type = battery_type

my_car = ElectricCar("Ford", "Blue", 100, "molten salt")

...


#8

Thank you denisaltroy. Your post was a great help. I'll remember to use the copy and paste trick for next time too thanks. :smile:


#9

i read about that on some other tutorial, its true however i didnt read that on the tutorial, maybe i missed somenthing


#10

Thank you so much for your explanation. very straightforward!


#11
    class Car(object):

    condition = "new"

    def __init__(self, model, color, mpg):
        self.model = model
        self.color = color
        self.mpg   = mpg
    
    def display_car(self):
        print "This is a %s %s with %s MPG." % (self.color, self.model, str(self.mpg))
    
    def drive_car(self):
        self.condition = "used"
    
    
class ElectricCar(Car):
    def __init__(self, battery_type):
        Car.__init__(self, model, color, mpg)
        self.battery_type = battery_type


my_car = Car("DeLorean", "silver", 88)
my_car = ElectricCar("Aston Masrtin", "Chicken poo", "777", "molten salt")
print my_car.battery_type
print my_car.condition
print my_car.model
print my_car.color
print my_car.mpg
print my_car.condition
my_car.drive_car()
print my_car.condition

tryed that correct way of inheritance, previously i just double typed in child class and it worked, but now its not working, error message is that i got 5 arguments but it only takes 2??

Traceback (most recent call last):
File "python", line 24, in
TypeError: init() takes exactly 2 arguments (5 given)

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


#12

Is it necessary to call all parent classes?
i wonder if this would work the same way:

class PlaneTicket(object):
    def __init__(self, seat_num):
        self.seat_num = seat_num

class EconomyClass(PlaneTicket): 
    def __init__(self, seat_num, snack):
        PlaneTicket.__init__(seat_num)
        self.snack = snack 

class BusinessClass(EconomyClass):
    def __init__(self, seat_num, snack, pillow)
        EconomyClass.__init__(seat_num, snack)
        self.pillow = pillow

class FirstClass(BusinessClass):
    def __init__(self, seat_num, snack, pillow, meal, wine):
        BusinessClass.__init__(seat_num, snack, pillow)
        self.meal = meal
        self.wine = wine

#13

Hi @courserunner82251,

Great question! There's only one way to know... Let's try! :wink:

Step into the Lab :microscope: to test it!

Hint: After adjusting it a little bit, I've actually replaced my own example code by yours... :smile:


#14

Hi @methodwhiz60695,

Go have a look at your modified code in this Lab room :microscope: and see if you understand why you were getting that error message... :smile:


#15

great, so i had the right thought.
and i see now that i forgot the self argument, had a similar problem in one of the python excercises.

also, thanks for making me aware there is a lab here to test stuff as well.

after runnig some testing, do you know if there is any difference when using them, or is it simply a cleaner code?


#16

@courserunner82251

Both ways works. I'm assuming the last inherited class's arguments will simply overwrite the previous ones. The difference is indeed cleaner, simpler code that make better use of the DRY principle.

I had not put much thoughts into my original example and had not even tried to run it as it was more of a goofy way to show how to use the trick to format code in the forum. I was glad to correct it as I realised some people had taken it as example for inheritence and it was not accurate, but now it is!


#17

i just remembered that i got here because my original attempt to solve this with using super() failed, so i used your way of doing it.
i want to make it work with super() as well but i have to do some research on how it works exactly. just wanted to mention it here that there is another solution to it (or should be).

you replied so fast and i have another (not to this topic related) question, i hope you dont mind.
i just finished the python course and wanted to get into another one, in this case java, but it seems ther is only 1 excercise available while everything else is locked. does that mean it is only available for pro members or are the courses not ready yet?
i thought everything except the additional content is free, and i couldnt find an answer to my question anywhere.


#18

@courserunner82251,

Click here for Official Python 2.x Doc for super()

Note that the 3.x syntax has changed a little bit so make sure you use the right one. Codecademy runs a few different minors of the 2.7.x branch. Use the following to find out what version a given interpreter is using:

import sys
print sys.version

Also, your implementation with super() might work in the lab but not in the python lessons though, since code gets tested for validity differently in the lessons

As for the Java learning path, lessons are still free, it's only the projects who are Pro-only. You will need to pass one unit to move on to the next though.

Have fun :smile:


#19

oh, i see.
i coult switch to units i didnt do yet in the python course so i didnt even think about it beeing diffrent with java.
thanks for the enlightment.


#20

This here worked for me after initializing the parent class within the child class

> class Car(object):
>     condition = "new"
>     def __init__(self, model, color, mpg):
>         self.model = model
>         self.color = color
>         self.mpg   = mpg
>     def display_car(self):
>         print "This is a %s %s with %s MPG."\
>         % (self.color, self.model, str(self.mpg))

>     def drive_car(self):
>         self.condition = "used"

> class ElectricCar(Car):
>     def __init__(self,battery_type, model, color, mpg):
>         Car.__init__(self, model, color, mpg)
>         self.battery_type = battery_type

> my_car = ElectricCar("molten salt", "Bmw", "Red", "40")