[Tutor] Help with Multiple Inheritance in Classes

Alan Gauld alan.gauld at yahoo.co.uk
Thu Feb 9 05:02:55 EST 2017


On 09/02/17 09:25, Vusa Moyo wrote:

> class Cat:
>     name = ""
>     kind = "cat"
>     color = ""
>     value = 100.00
> 
>     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
> 
> The above code is the question, which I am not allowed to edit.
> 
> So just to test the lecturer's code, I run the command
> 
> print(Cat.description())

But the definition of description() take an argument - self.
self is expected to be an instance of Cat.
You can either pass that in manually

print( Cat.description(Cat()) )

or, more normally, create an instance of cat and call
description on that:

my_cat = Cat()
print( my_cat.description() )

> Any other code I append to it by inheriting the class Cat, will still  have
> that similar error.

I'm not sure what you mean by that, I'd need an example.
If you mean you just add the code after the above line then
obviously you will still get the error.

> Now, I've added the following code to inherit the class Cat: description.
> 
> class Cat1(Cat):
>     name = "Whiskers"
>     kind = "Burmese cat"
>     color = "grey"
>     value = 3000.00
> 
> When I run this command, I still receive the same error.
> 
> print(Cat1.description())

For the same reason; you are still not passing an instance
of Cat (or Cat1) to the method. You need to create an
instance and then call the method on that:

other_cat = Cat1()
print( other_cat.description() )


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list