简体   繁体   中英

Unresolved Reference 'Object' problem with getting variables from class

Hi everyone I am having an unresolved reference error. My code looks like this

class Main:
    PhysicalDmg = 0
    MagicDmg = 0
    dmgTaken = 100
    Health = 100

class HeroSelection(Main):
    dyl = Dylan()                    <<<<<<<<<<<<<<<<<ERROR LINE
    user_hero_selection = input("choose your fighter")
    if user_hero_selection == "Dylan":
        print(dyl.stats)

class Dylan(Main, HeroSelection):
    print("character DYLAN")
    stats = ["Dylan", 24, 6.5, 210, "Physical", 2]

I am trying to print the 'stats' array from the Dylan Class into the user_hero_selection class. But the Pycharm keeps on giving me the error: unresolved reference 'Dylan'. I've been struggling on this so any help would be greatly appreciated. Thank you and have a good day.

Python reads a file from top to bottom and runs all the global level code which includes class level definitions in your code.

Since Dylan() is a class level definition of the HeroSelection class, it means it will run when python runs this script, however, since class Dylan is defined after the class level definition, it does not know that Dylan exists yet.

It also looks like if you move class Dylan above HeroSelection you have the same problem, since you reference HeroSelection as a base class of Dylan

A quick fix is to put an initializer __init__ for HeroSelection

class HeroSelection(Main):
    def __init__(self):
        dyl = Dylan()
        user_hero_selection = input("choose your fighter")
        if user_hero_selection == "Dylan":
            print(dyl.stats)

However, not sure how you are using this classes.

More on execution model and resolution found here and class definition and instances here

You are putting code into the initialization of a Class, so any variables you set there will be available to all instances of a class (consider that for every class Main: you can have many main1 = Main() and they will all see the same PhysicalDmg , whereas what you probably wanted was something like def __init__(): self.PhysicalDmg which sets the variable on the specific instance main1

consider this approach:

class Stats:
  def __init__(self, pd=0, md=0, dt=100, h=100):
    self.PhysicalDmg = pd
    self.MagicDmg = 0
    self.dmgTaken = dt
    self.Health = h
  def __str__(self):
    return [self.PhysicalDmg, self.MagicDmg, self.dmgTaken, self.Health]

# this doesnt need to be a class
def HeroSelection():
    dyl = Dylan()
    user_hero_selection = raw_input("choose your fighter") # use raw input in py27
    if user_hero_selection.strip() == "Dylan":
        print(str(dyl.stats))

class Dylan(Main):
  def __init__(self):
    print("character DYLAN")
    self.name = "Dylan"
    self.stats = Stats(24, 6.5, 210, "Physical", 2)

HeroSelection()

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