Classes Inheritance

Hi there. How do i pass arguments to get_make_model in the for loop below:

class vehicle():
    def __init__(self, make, model):
        self.make = make
        self.model = model

    def moves(self):
        print("Moves along...")

    def get_make_model(self, whose_object, what_object):
        print(f"{whose_object}\'s {what_object} is {self.make} {self.model}")

class airplane(vehicle):
    def moves(self):
        print("Flies along...")

class truck(vehicle):
    def moves(self):
        print("Rumbles along...")

# This code works fine
maditsi_airplane = airplane("Boing", "707")
maditsi_airplane.get_make_model("Maditsi", "airplane")
maditsi_airplane.moves()

# This code works fine
mosebo_truck = truck("Volvo", "SD200")
mosebo_truck.get_make_model("Mosebo", "truck")
mosebo_truck.moves()

# I get not defined error here
for v in (mosebo_truck, maditsi_airplane):
    v.get_make_model(whose_object, what_object)
    v.moves()

Because, well, whose_object and what_object don’t exist here. Where are you assigning them values?

1 Like

Hi there. How do i pass arguments to get_make_model in the for loop
below:

class vehicle():
   def __init__(self, make, model):
       self.make = make
       self.model = model
[...]
   def get_make_model(self, whose_object, what_object):
       print(f"{whose_object}\'s {what_object} is {self.make} {self.model}")

Note that this function just prints stuff out.

I get not defined error here

for v in (mosebo_truck, maditsi_airplane):
   v.get_make_model(whose_object, what_object)
   v.moves()

That is because mosebo_truck and maditsi_airplane are not defined.

When you write a function like this:

 def add(a, b):
     return a + b

the names a and b are known only within the function, just for the
duration of the function. To get values back from the function you
should return them, as we do above, returning the sum a+b.

Using it is like this:

 x = 2
 y = 5
 z = add(x, y)
 print(z)

The value of z should come out as 7. Notice that the main code has
variables x and y and that the add() function doesn’t know
anything about them. When you call add(x,y) the values of x and y
are assigned to the functions local variables a and b respectively.
The function uses those variables to compute the sum and returns the
sum. The local variables are discarded.

Your main code doesn’t know anything about a and b - they’re known
only inside the function.

When you call get_make_model() the values you pass in are assigned to
local variables whose_object and what_object and used to do the
print(). Then you return from the function and the local variables are
discarded.

So in your main code with the for-loop, these names are not known.

Can you tell use what you intended to do in the for-loop? Where do the
values of whose_object and what_object come from in the main code -
they are unrelated to the values you used for your calls to
get_make_model().

I am just guessing, but maybe your task involves storing the whose
and what values on each vehicle, and that the get_make_model()
method should print those values, obtained from the vehicle?

If so I’d have 2 methods: a set_make_model method to store those
values, and change get_make_model to get the values from the vehicle
i.e. self, eg as self.whose.

In that case, the set_make_model would accept the whose and what
values and store then on the vehicle (just like __init__ stores the
make and model on the vehicle). And get_make_model would take no
arguments and instead access eg self.whose and so on.

2 Likes

Hi,
Values are supposed to be passed here. But how do i pass the values in a loop since there’s more than one?

maditsi_airplane = airplane("Boing", "707", "FAA-7689")
maditsi_airplane.get_make_model("Maditsi", "airplane")
maditsi_airplane.moves()
print("")

mosebo_truck = truck("Volvo", "SD200")
mosebo_truck.get_make_model("Mosebo", "truck")
mosebo_truck.moves()
for v, whose_object,what_object in [(mosebo_truck,"Mosebo", "truck") , (maditsi_airplane, "Maditsi", "airplane")]:
    v.get_make_model(whose_object, what_object)
    v.moves()
1 Like