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


Messages In This Thread
Calling a base class variable from an inherited class - by CompleteNewb - Jan-19-2022, 05:39 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  class Blockage not projecting Azdaghost 1 456 May-15-2025, 04:32 PM
Last Post: deanhystad
  Return a string or byte object from Enum class? Calab 5 796 May-14-2025, 05:21 PM
Last Post: snippsat
  Python inner classes inheritance from parent class Abedin 8 1,364 Apr-23-2025, 05:56 AM
Last Post: Gribouillis
  Accessing method attributes of python class Abedin 6 1,413 Apr-14-2025, 07:02 AM
Last Post: buran
  Python class members based on a type value voidtrance 7 1,559 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,687 Mar-25-2025, 06:37 PM
Last Post: voidtrance
  printing/out put issue with class arabuamir 3 1,171 Aug-25-2024, 09:29 AM
Last Post: arabuamir
  Class test : good way to split methods into several files paul18fr 5 4,289 Jul-17-2024, 11:12 AM
Last Post: felixandrea
  [split] Class and methods ebn852_pan 15 3,734 May-23-2024, 11:57 PM
Last Post: ebn852_pan
  [SOLVED] [listbox] Feed it with dict passed to class? Winfried 3 1,446 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