1

I have created a class Character and a subclass Player. I want to create an instance of the Player subclass but specify the self.variables during instantiation.

class Character():
    def __init__(self):
        self.ID = None
        self.Name = None
        self.Colour = None

class Player(Character):
    def __init__(self, ID, Name, Colour):
        self.ID = ID
        self.Name = Name
        self.Colour = Colour
        self.Karma = 0
        self.Charisma = 0
        self.Strength = 0
        self.Intelligence = 0

I can easily create an instance by using Player("tutorial_guy", "Tutorial Ted", "Green") but then that defeats the whole point of a subclass because of how the subclass Player is set up. How would I make the variables of ID, Name and Colour be set in Character but still instantiated the same way?

nx_prv
  • 127
  • 8
  • 1
    Are you asking how to reuse the parent class constructor in the child class? – MisterMiyagi Aug 03 '19 at 17:14
  • 2
    what is `NPC` you described in the question? What you are describing is simply the basic concept of inheritance. Your `Character` init method should take the required arguments and assign them, and then the `Player` init will call `super()` with those arguments to reuse the code – Tomerikoo Aug 03 '19 at 17:15
  • 2
    Possible duplicate of [Inheritance and init method in Python](https://stackoverflow.com/questions/5166473/inheritance-and-init-method-in-python) – MisterMiyagi Aug 03 '19 at 17:17
  • My question is based around using `super()`, I don't know how to implement it – nx_prv Aug 03 '19 at 17:26
  • @Tomerikoo I just posted an answer to that effect – wjandrea Aug 03 '19 at 17:47

2 Answers2

2

There's no reason to subclass Character if it doesn't really do anything. So let's make it set ID, Name, Colour and then use super() to call its __init__ method.

(UpperCamelCase names should be reserved for classes, so I'm using id_, name, colour -- id_ to avoid shadowing the builtin id().)

class Character():
    def __init__(self, id_, name, colour):
        self.id_ = id_
        self.name = name
        self.colour = colour

class Player(Character):
    def __init__(self, id_, name, colour):
        super().__init__(id_, name, colour)
        self.karma = 0
        self.charisma = 0
        self.strength = 0
        self.intelligence = 0

p.s. I'm just getting into OOP so please lmk if I'm confused

wjandrea
  • 28,235
  • 9
  • 60
  • 81
0

Include the attribute as a class Character() inside Player():

class Character():
    def __init__(self):
        self.ID = None
        self.Name = None
        self.Colour = None
    def setAttributes(self, ID, Name, Colour) :
        self.ID = ID
        self.Name = Name
        self.Colour = Colour
    def showAttributes(self) :
        print(self.ID)
        print(self.Name)
        print(self.Colour)

class Player(Character):
    def __init__(self, ID, Name, Colour):
        self.char = Character()
        self.char.setAttributes( ID, Name, Colour )
        self.ID = ID
        self.Name = Name
        self.Colour = Colour
        self.Karma = 0
        self.Charisma = 0
        self.Strength = 0
        self.Intelligence = 0

a = Player("me", "Tim", "red")

### OUTPUT
>>> a.showAttributes()
me
Tim
red

if you want to keep the same initialization configuration, otherwise you can specify the Character char at __init__ etc

cccnrc
  • 1,195
  • 11
  • 27