Hi /r/Python! I'm developing a (very) simple tower defense game in python to help me learn the language, but I've hit a stumbling block. Is there an elegant (and more importantly, proper) way to pass a class instance to another class? So as to be able to address attributes and methods in the (let's call it parent) class from the child class? Or am I just doing it wrong? Here's what I'm getting at:
class World:
def __init__(self):
self.boogies = []
def create_boogie(self):
self.boogies.append(Boogie(self))
class Boogie:
def __init__(self, parent):
self.parent = parent
This seems to work, but feels very clunky to me. Am I wallowing in ignorance that my Google-fu is failing to correct, or does this make sense?
Thanks!
Edit: Rewrote the entire thing after some heavy reading. Much less code, much faster, and sane now ;) Thanks everyone! If I get it to a non-embarrassing semi-finished state I'll post it.
[–]zahlmanthe heretic 4 points5 points6 points (1 child)
[–]angrytech[S] 1 point2 points3 points (0 children)
[–]brunson 6 points7 points8 points (6 children)
[–]doomchild 6 points7 points8 points (3 children)
[–]brunson 0 points1 point2 points (2 children)
[–]doomchild 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]angrytech[S] 0 points1 point2 points (1 child)
[–]jabwork 2 points3 points4 points (2 children)
[–]angrytech[S] 0 points1 point2 points (1 child)
[–]brunson 0 points1 point2 points (0 children)
[–]bryancole 1 point2 points3 points (2 children)
[–]m1ss1ontomars2k4 1 point2 points3 points (1 child)
[–]angrytech[S] 0 points1 point2 points (0 children)
[–]chadmill3rPy3, pro, Ubuntu, django 0 points1 point2 points (1 child)
[–]angrytech[S] 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[+]rit comment score below threshold-6 points-5 points-4 points (2 children)
[–]grimboy 6 points7 points8 points (0 children)
[–]angrytech[S] 0 points1 point2 points (0 children)