[Tutor] Help with Multiple Inheritance in Classes

Peter Otten __peter__ at web.de
Wed Feb 8 03:29:28 EST 2017


Vusa Moyo wrote:

> I have a suspicion my lecturer's question is flawed, so I'd like to pose
> it to you guys to confirm my suspicions.
> 
> Here goes..
> 
> I've gone and created a Class Cat1(cat): <-- inherited class, but cant
> seem get the code right which allows the test code to run successfully.

Is the following... 

> We have a class defined for cats. Create a new cat called cat1. Set cat1
> to be a grey Burmese cat worth 3000 with the name Whiskers.

your task? Then your lecturer may be asking you to *instantiate* the Cat 
class, not to make a subclass.

>  # define the Cat class
> 
>  class Cat:
>      name = ""
> 
>      kind = "cat"
>      color = ""
>      value = 100.00

The above are all class attributes. This is unusual, as even two cates of 
the same race will have a different name and value. If I'm guessing right 
your solution will look more like

$ cat cars.py
class Car:
    def __init__(self, make, color, year):
        self.make = make
        self.color = color
        self.year = year

    def __str__(self):
        return "{0.color} {0.make}".format(self)

    def __repr__(self):
        return (
            "Car(color={0.color}, "
            "make={0.make}, "
            "year={0.year})"
        ).format(self)

carpark = [
    Car("Ford", "blue", 1973),
    Car("Volkswagen", "yellow", 2003)
]

print(carpark)  # uses Car.__repr__
print()

print("In our car park we have")
for car in carpark:
    print("    - a", car)  # uses Car.__str__
$ python3 cars.py 
[Car(color=blue, make=Ford, year=1973), Car(color=yellow, make=Volkswagen, 
year=2003)]

In our car park we have
    - a blue Ford
    - a yellow Volkswagen
$ 

>From the view of the script a VW and a Ford work the same, so there is no 
need to introduce subclasses, but if you're asked to do it anyway here's one 
way:

$ cat cars2.py
class Car:
    def __init__(self, color, year):
        self.color = color
        self.year = year

    def __str__(self):
        return "{0.color} {0.make}".format(self)

    def __repr__(self):
        return (
            "{classname}(color={0.color}, "
            "year={0.year})"
        ).format(self, classname=self.__class__.__name__)

class Ford(Car):
    make = "Ford"

class Volkswagen(Car):
    make = "VW"

carpark = [
    Ford("blue", 1973),
    Volkswagen("yellow", 2003)
]

print(carpark)  # uses Car.__repr__
print()

print("In our car park we have")
for car in carpark:
    print("    - a", car)  # uses Car.__str__
$ python3 cars2.py 
[Ford(color=blue, year=1973), Volkswagen(color=yellow, year=2003)]

In our car park we have
    - a blue Ford
    - a yellow VW
$ 

However, this is the kind of inheritance you will only ever see in textbook 
examples because the subclasses do not introduce differing features. As a 
rule of thumb there should be at least one method with a fundamentally 
different implementation, or one extra method that is not shared between 
baseclass and subclass.

>      def description(self):
> desc_str = "%s is a %s %s cat worth R%.2f." % (self.name, self.color,
> self.kind, self.value)
> 
>          return desc_str
> 
> # your code goes here
> 
> 
> # test code
> 
>  print(cat1.description())





More information about the Tutor mailing list