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
  class Blockage not projecting Azdaghost 1 369 May-15-2025, 04:32 PM
Last Post: deanhystad
  Return a string or byte object from Enum class? Calab 5 655 May-14-2025, 05:21 PM
Last Post: snippsat
  Python inner classes inheritance from parent class Abedin 8 1,248 Apr-23-2025, 05:56 AM
Last Post: Gribouillis
  Accessing method attributes of python class Abedin 6 1,354 Apr-14-2025, 07:02 AM
Last Post: buran
  Python class members based on a type value voidtrance 7 1,486 Apr-11-2025, 10:10 PM
Last Post: deanhystad
  Create a new subclass in a Python extension based on an existing class voidtrance 6 1,584 Mar-25-2025, 06:37 PM
Last Post: voidtrance
  printing/out put issue with class arabuamir 3 1,134 Aug-25-2024, 09:29 AM
Last Post: arabuamir
  Class test : good way to split methods into several files paul18fr 5 4,217 Jul-17-2024, 11:12 AM
Last Post: felixandrea
  [split] Class and methods ebn852_pan 15 3,647 May-23-2024, 11:57 PM
Last Post: ebn852_pan
  [SOLVED] [listbox] Feed it with dict passed to class? Winfried 3 1,416 May-13-2024, 05:57 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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