0

I'm playing around with OOP in Python and I am trying to figure out some stuff related to inheritance. . I have some code here that has a few classes. A class called Blacksmith which behaves as expected and a class called Hero which I am trying to call a function from but I recieve an unexpected output.

class Character(object):
    def __init__(self,name):
        self.health=100
        self.name = name
    #   self.player = player
    def printName(self):
        print self.name
    #def printPlayerName(self):
    #   print self.player

class Blacksmith(Character):
    def __init__(self,name, forgeName):
        super(Blacksmith, self).__init__(name)
        #self.name = "Billy"
        self.forge = Forge(forgeName)

class Hero(Character):
    playerName = "Player"
    def __init__(self,name):
        super(Hero, self).__init__(name)
    def setplayername(self,inputplayername):
        playerName = inputplayername

class Forge:
    def __init__(self,forgeName):
        self.name = forgeName

bs = Blacksmith("Billy", "Billy's Forge")
print bs.health
bs.printName()
print bs.forge.name
player1 = Hero("Methos")
print player1.name
player1.setplayername("Chris")
#print playher1.playerName
print player1.playerName

Output is:

raina@DESKTOP-291MTC0 ~/python
$ python learningoopclasses01.py
100
Billy
Billy's Forge
Methos
Player

Can anyone explain why this output says "Player" and not "Chris". Another question I have is I am not entirely sure how the init methods work. What does super do in these cases? What does calling init with a name value do exactly? Thanks.

2
  • 1
    Turns out the issue was actually fixed by changing this line: playerName = inputplayername to self.playerName = inputplayername. My other questions still stand, however. Commented Jul 29, 2018 at 19:48
  • What's the other question? Please edit your question to update it before someone spends time on solving a problem which is already gone. Commented Jul 29, 2018 at 19:57

3 Answers 3

2

__init__ is called when an object of that Class is created. With this method, we will also use the self variable to represent the instance of the object itself. It has to be explicitly declared in Python to be defined on an object.

For example,

class Student():
    def __init__(self, score1, score2, score3):
        self.scores = [score1, score2, score3]

If you want to assign scores to Student 1, you would only need to use because stu_1 already has score as an attribute in it:

stu_1 = Student(80, 90, 85)

In addition, __init__ also will notify you if any parameters are entered incorrectly according to what it has been set up.

super() is used to first call the parent(super) class of Blacksmith, which is Character, and allows you access Character's property.

Sign up to request clarification or add additional context in comments.

Comments

0

The call to super() in Blacksmith's __init__ method is equal to its superclass, which in this case is Character.

You could also replace super(Blacksmith, self).__init__(name) with Character.__init__(self, name). The two are equivalent.

Comments

0

A slight adjustment to

def setplayername(self,inputplayername):
    self.playerName = inputplayername

In the Hero class will fix it, if you don't want to change anything else.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.