I want to make a class where one object in that class belongs to a different type of class (though not in a nested way), and the second class object can point back to the class object that contains it.
That's pretty abstract. Like, let's say I want a class called "Party", which contains objects of the class Character. Let's say that Party has an int object called Total_gold. I know I could create that using something like:
class Party(object):
def __init__(self, G1, c1, c2, ...other_stuff_goes_here):
self.Total_gold = G1;
self.char1 = Character(c1);
self.char2 = Character(c2);
# more stuff to initiate Party
class Character(object):
def __init__(self, other_stuff_goes_here):
# stuff to initiate Character
Now, let's say I have a function that takes in a Character object, but I want it to alter the Party that contains that Character. For example, let's say I want to have a function, Spend, that decreases the Party's Total_gold. Is there a way for Spend to take in a Character object, and then access the Party the Character is in?
Something like:
def Spend(character, amount):
#reduces the Total_gold of the party the character belongs to.
character.Party.Total_gold -= amount
If so, how would I do this? I assume I need to put something in the __init__ function, but I don't know how I would do that. Python doesn't have pointers, right?
If another post has covered this, please let me know. I wasn't sure how to even ask this.
Thank you.
Party.Total_golda@propertythat sums all the characters' gold.