I don't know if it is about me or the section


#1



I've saw a lot of strange and kind of silly things in the forum. This exercise is for inheritance but everybody in the forum is just rewriting /self.model = model/ and etc. Which is not saving time and makes this exercise pointless. The other thing I saw is is deleting this: Car("DeLorean", "silver", 88), but I don't think this will be helpful in future plan! Can someone explain if I am missing something or something else is wrong! This is the error message I get:
Oops, try again. my_car does not appear to be an instance of ElectricCar.
I think my indentation is right but not a 100% sure...
Thanks for the time and thanks in advance!!


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"
        return self.condition

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

class ElectricCar(Car):
    def __init__(self, model, color, mpg, battery_type):
        self.battery_type = battery_type
        
my_car = ElectricCar("Malibu 2.0", "blue", 120, "molten salt")
print my_car


#2

make sense, if you don't give electric car a init method, the parent init method is called, if you decide to give ElectricCar a init method, you have to re-initialize all the instance variables (self.model and so on)

using super() to call parent method would be a good start. But this hasn't been taught yet i think


#3

I've seen super() in the previous section but I didn't quite understand when and how to use it! Chcked the forum and saw some people using the super() but after copying their statements I've got a syntax error. As I remember the super() should look somthing like this:
super(ElectricCar, self).init(model, color, mpg) but got a syntax error(not 100% sure about that) and an error that says: self is not defined!


#4

you can use super() to call parent method, did you place super() in __init__() method of ElectricCar? Please show full code


#5

I've tried using super but because the error saying that self is not defined I deleted it! But if I can do a blind guess (I mean and I don't understand why) super() should be used to call model, color and etc!


#6

well, you need to initialize the instance variable (self.model) and so on.

What specific help do you need from me at this point?


#7

So, the super() is used in child classes to call from the parent class, right? And if I am right can you explain to me how the super() looks?


#8

no, super() is used to call method of parent class, this is right:

super(ElectricCar, self).__init__(model, color, mpg)

did you place it in the init method of child class?


#9

class ElectricCar(Car):
def init(self, model, color, mpg, battery_type):
self.battery_type = battery_type
super(ElectricCar, self).init(model, color, mpg)
my_car = ElectricCar("Malibu 2.0", "blue", 120, "molten salt")
print my_car

This is what I did I I don't know why but after refreshing the browser it worked...


#10

maybe your browser was sending cached data, anyway, problem solved :slight_smile:


#11

Yep, thank you for 3rd time today!! I hope I won't need to bother someone in this forum again any time soon! :smiley:


#12

You're welcome :slight_smile:

will see, classes can be difficult to wrap your head around.


#13

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