Python Forum
Calling a base class variable from an inherited class
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Calling a base class variable from an inherited class
#1
I have this little program

class Generic_Player:

    def __init__(self):
        self.hit = True

    def Bust(self):
        print(self.name + " busts.")

class Player(Generic_Player):

    def __init__(self, name):
        self.name = name

    def Win(self):
        print(self.name + " wins!")

    def Lose(self):
        print(self.name + " loses.")

player1 = Player("player1")

player1.Bust()
print(player1.hit)
which gives me this error

Error:
Traceback (most recent call last): File "/usr/lib/python3.8/idlelib/run.py", line 559, in runcode exec(code, self.locals) File "/home/fook/Documents/pygame/opencv/test.py", line 43, in <module> print(player1.hit) AttributeError: 'Player' object has no attribute 'hit'
Since player1 is inherited from Generic_Player, True should be printed, no?
Reply
#2
You have to have your init call the base class init.

class Player(Generic_Player):

    def __init__(self, name):
        super().__init__()
        self.name = name
Gribouillis likes this post
Reply
#3
A subclass needs to call the __init__() method(s) of its superclass(es) to so they can initialize their instance variables. In this case Player.__init__() does not call GenericPlayer.__init__(). Since GenericPlaier.__init__() was never called this code was never executed:
        self.hit = True
self.hit is never assigned, so there is no "hit" attribute in player1.

To call the superclass __init__() you should use the super().__init__(self) as recommended by bowlofred. Sometimes you will see this done using the superclass name explicitly.
class Player(Generic_Player):
    def __init__(self, name):
        GenericPlayer.__init__(self)
        self.name = name
bowlofred likes this post
Reply
#4
oh okay thanks greatly appreciated
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How does this code create a class? Pedroski55 4 143 15 minutes ago
Last Post: Gribouillis
  class definition and problem with a method HerrAyas 2 237 Apr-01-2024, 03:34 PM
Last Post: HerrAyas
  Printing out incidence values for Class Object SquderDragon 3 276 Apr-01-2024, 07:52 AM
Last Post: SquderDragon
  class and runtime akbarza 4 358 Mar-16-2024, 01:32 PM
Last Post: deanhystad
  Operation result class SirDonkey 6 504 Feb-25-2024, 10:53 AM
Last Post: Gribouillis
  The function of double underscore back and front in a class function name? Pedroski55 9 645 Feb-19-2024, 03:51 PM
Last Post: deanhystad
  super() and order of running method in class inheritance akbarza 7 725 Feb-04-2024, 09:35 AM
Last Post: Gribouillis
  Class test : good way to split methods into several files paul18fr 4 473 Jan-30-2024, 11:46 AM
Last Post: Pedroski55
  Good class design - with a Snake game as an example bear 1 1,828 Jan-24-2024, 08:36 AM
Last Post: annakenna
  question about __repr__ in a class akbarza 4 607 Jan-12-2024, 11:22 AM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

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