Python Forum
Object inheriting from two classes?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Object inheriting from two classes?
#1
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?
Reply
#2
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
Melendroach likes this post
Reply
#3
(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!
Reply
#4
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Inheriting, given number from randint is not working beLIEve 0 1,517 Oct-12-2019, 06:50 PM
Last Post: beLIEve
  Return Object Created from Multiple Classes emerger 3 3,066 Oct-18-2018, 02:14 AM
Last Post: emerger
  Issue python3.6 while inheriting telnet library sourabhjaiswal92 4 4,101 May-09-2018, 05:20 AM
Last Post: sourabhjaiswal92
  Using classes? Can I just use classes to structure code? muteboy 5 5,061 Nov-01-2017, 04:20 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020