1

Ok so lets say I have two child classes and a parent class. How would I compare similarities and differences between the two child classes? for example

class vehicles:
    def __init__(self, make, model):
        self.make = make
        self.model = model
class truck(vehicles):
    def __init__(self, make, model):
        vehicles.__init__(self, make, model)
        self.make = "ford"
        self.model = "F-150"
class cars(vehicles):
    def __init__(self, make, model):
        vehicles.__init__(self, make, model)
        self.make = "ford"
        self.model = "mustang"

how would I get it to print what the two child classes have in common here? I have no idea where to start here. I've looked on other sites and read through my python book but I can't find where it shows me how to do this or I'm just not understanding it. what I'm trying to compare are the objectd of make and model. I didn't know I didn't need the extra self.'s but I'm still not sure what to do here.

  • What have you tried so far based on your own research, and what went wrong with your attempts? Please [edit] to include a [mcve] with your comparison code – G. Anderson Oct 27 '21 at 18:15
  • I can't provide a minimal reproducible example because I don't know where to start or even if what I'm trying to do is possible with this data type. That's why I'm asking how to do it. – reagent -52 Oct 27 '21 at 18:26
  • 1
    what exactly do you want to compare (values or attributes or what? is the comparison based on the attributes that parent class has? so only compare the values of inherited attributes?), besides in both child classes you don't need to use `self.make` and `self.model`, just pass the arguments to parent `__init__` method, otherwise what is the point of inheritance? – Matiiss Oct 27 '21 at 18:52
  • ok I didn't know about that. So what do I do to assign values to the objects then? – reagent -52 Oct 27 '21 at 19:04
  • @reagent-52 in your case with this specific code in the `__init__` methods of inheriting classes you only need to type `vehicles.__init__(self, "ford", "mustang")` and for the other `vehicles.__init__(self, "ford", "F-150")`, besides there is no point of putting args for the user since they will be overriden – Matiiss Oct 27 '21 at 19:14

1 Answers1

2

You could create a method for Vehicle (name adjusted for PEP 8 convention) class (because it is the base class so inheriting classes will inherit this method) that will compare the model and make of itself and the model and make of passed argument (and by compare here is meant that it will just return both of those grouped in a tuple because you didn't specify how to compare exactly):

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

    def compare_make_model(self, other):
        if isinstance(other, Vehicle):
            return (self.make, other.make), (self.model, other.model)
        else:
            raise TypeError(f'Expected Vehicle got \'{type(other).__name__}\' instead')


class Truck(Vehicle):
    pass


class Car(Vehicle):
    pass


f150 = Truck('ford', 'F-150')
mustang = Car('ford', 'mustang')

print(f150.compare_make_model(mustang))

As you can see then child classes inherit this method and it can be used on them to compare the make and model. Also as you notice nothing has to be done with Car and Truck because really no new behavior is added so everything gets inherited (so currently there really is no point in creating those two child classes but I guess that you will add some additional behavior later).

Also:
I strongly suggest following PEP 8 - Style Guide for Python Code. Function and variable names should be in snake_case, class names in CapitalCase. Have two blank lines around function and class declarations. Class methods have one blank line around them.

Sources:

Matiiss
  • 5,970
  • 2
  • 12
  • 29
  • ok Thanks. I had another question but I thought it's be better to ask here than create a whole new question. say I have a list of classes with inheritance like you made above. Is there any way to print the individual class names besides just putting them in a print function? like say I wanted to print the classes leading up to the truck class? how would I do that? – reagent -52 Oct 27 '21 at 21:06
  • 1
    @reagent-52 something like this: [list all base classes in a hierarchy of given class](https://stackoverflow.com/questions/1401661/list-all-base-classes-in-a-hierarchy-of-given-class)? – Matiiss Oct 27 '21 at 21:11
  • Yes that's probably what I'm looking for thank you – reagent -52 Oct 27 '21 at 21:13