Kelvin Davis Kelvin Davis - 12 days ago 5
Python Question

Issue with passing attributes between classes in python

Ive made these classes below and I'm trying to have a tile in the game present a text based on an attribute in another class. I keep getting this error.
File "C:\Users\xxxxxxxx\PycharmProjects\Game.idea\Girls.py", line 20, in not_interested
return (self.interest < 10)
AttributeError: 'GirlTile' object has no attribute 'interest'

class Girls():
def __init__(self):
self.girlnames = ["Lucy", "Cindy", "April", "Allison", "Heather", "Andrea", "Brittany", "Jessica", "Lane", "Lauren", "Sabrina","Chelsea","Amber"]
self.name = random.choice(self.girlnames)
self.height = random.randrange(60, 72)
self.age = random.randrange(18, 25)
self.number = self.new_number()
self.interest = 0

def not_interested(self):
return (self.interest < 10)

from Girls import Girls

class GirlTile(MapTile):
def __init__(self,x,y):
self.enemy = Girls()
super().__init__(x, y)

def intro_text(self):
self.stance = Girls.not_interested(self)
if self.stance:
print("Hey whats up")

FMc FMc
Answer

It looks like not_interested is an instance-level method, but you are trying to call it with a class (Girls). And the call is sort of "working" because you are passing the GirlTile instance in the call -- hence the error that the GirlTile has no interest attribute (because it does not).

Maybe you intended this instead?

def intro_text(self):
    # Use an actual Girl instance to call not_interested().
    self.stance = self.enemy.not_interested()
    ...
Comments