Python Forum
Object inheriting from two classes? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Object inheriting from two classes? (/thread-33627.html)



Object inheriting from two classes? - Melendroach - May-11-2021

So let's say I have 10 'roles' and 10 'races', I'd like to make each of them into a class.
Every character (player, npc, whatever) has a role and also has a race.

How can I make an object inherit functions and variables from two classes, while also making it possible to create connections between the classes? (As in, let's say one race changes the way something in role works). Obviously I don't want to create 100 subclasses that inherit from each role and each race.

I know one solution is to just make every object (npc, player) a tuple (or a list) with one element being a role and the other a race, but I was wondering if there is an alternative?

If not, I can do with it being a tuple. In that case, are there any things I should look out for / remember about?


RE: Object inheriting from two classes? - deanhystad - May-11-2021

This is not multiple inheritance, it is aggregation. You will make a class for your characters and that class will have a race attribute and a role attribute (and probably other attributes as well)
class Character:
    def __init__(self, race, role):
        self.race = race
        self.role = role

elfThief = Character(Elf, Thief)
You can think of classes as being a lot like a dictionary. A class can have many attributes and the attributes are accessed using their name. The syntax for accessing the attributes is cleaner than a dictionary, and you can use inheritance to make subclasses that share attributes of the original class. For example, you might make a class of NPC which is a subclass of Character
class NPC(Character):
    def __init__(self, race, role, health):
        super().__init(race, role)
        self.health = health



RE: Object inheriting from two classes? - Melendroach - May-11-2021

(May-11-2021, 04:20 PM)deanhystad Wrote: This is not multiple inheritance, it is aggregation. You will make a class for your characters and that class will have a race attribute and a role attribute (and probably other attributes as well)
class Character:
    def __init__(self, race, role):
        self.race = race
        self.role = role

elfThief = Character(Elf, Thief)
You can think of classes as being a lot like a dictionary. A class can have many attributes and the attributes are accessed using their name. The syntax for accessing the attributes is cleaner than a dictionary, and you can use inheritance to make subclasses that share attributes of the original class. For example, you might make a class of NPC which is a subclass of Character
class NPC(Character):
    def __init__(self, race, role, health):
        super().__init(race, role)
        self.health = health
Oh, right, of course!
I'm still quite new to OOP, but this seems like a good and elegant way to solve my problem.
Thanks a lot!


RE: Object inheriting from two classes? - deanhystad - May-11-2021

This is not OOP. It is using classes. OOP is a design philosophy. Classes are a Python language feature. You can do OOP without using classes, and you can use classes and not use OOP.