简体   繁体   中英

Python Child Class Instance is Unable to Access Parent Initiated Variables

Here is the background. I am making a game in python 3.7 using the Pyglet library. I am trying to access just a boolean variable called dead that is initiated in the parent class's __init__ function. In the child class, I have called the parent's __init__ function within the child's own __init__ . Whenever I try to access dead variable from the parent methods inherited by the child class, I keep getting an error that the child class cannot find the variable.

Parent class is called PhysicalObject within physicalobject.py:

import pyglet

class PhysicalObject(pyglet.prite.Sprite):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.dead = False

Child class is called Player within player.py (same directory as physicalobject.py. Directory is called game )

import pyglet
from game import physicalobject

class Player(physialobject.PhysicalObject):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        print(self.dead)

Finally from the main script file one directory level up, we bring it all together and create the player:

import pyglet
from game import player

p = player.Player()

I'm not sure why the child cannot see self.dead on the last line here print(self.dead) , but I know I am not understanding something right about inheritance. I am not used to python, but in other programming languages we define our variables first before __init__ and I know doing so in python will result in a static variable, but that seems to be the only way that I can get the result I am looking for. I have been searching for hours trying to get this to work going through every suggestion already asked on stack overflow with no luck.

Exact error below:

AttributeError: 'Player' object has no attribute 'dead'

EDIT: This was never technically answered, but when trying to do the exact same thing as bare bones as I could make it, I got that barebones script to work using the same styled syntax as above. I need to break down my project further to figure out exactly what is happening, but when I find out, I will be back to revise this post again.

import pyglet

class PhysicalObject(pyglet.sprite.Sprite):
  def __init__(self, *args, **kwargs):
    self.dead = False

class Player(PhysicalObject):
  def __init__(self, *args, **kwargs):
    super().__init__(*args, **kwargs)
    print(self.dead)

if __name__ == "__main__":
  p = Player()

It works, Try to import the PhysicalObject class properly.

import pyglet
from game import physicalobject

class Player(physialobject.PhysicalObject):

  def __init__(self, *args, **kwargs):
    super().__init__(*args, **kwargs)
    PhysicalObject.__init__(self)
    print(self.dead)

This solved it for me

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM