-1

I know this is wrong, but I don't know why, not how to do it right. I don't want myParent to be myChlid's superclass. I can't really find words to ask what I want to do so I didn't find any way to find an answer online. Maybe you can answer it :)

class myParent():
    def __init__(self, whatever):
        self.child = myChild(parent_ = self)
        self.myVar = whatever

class myChild():
    def __init__(self, parent_):
        self.parent_ = parent_
        print self.parent_.myVar

myParent(whatever)

It raises this error :

# AttributeError: myParent instance has no attribute 'myVar' #

2
  • Are you looking for something like this maybe? stackoverflow.com/questions/18006310/… Commented Feb 9, 2018 at 17:12
  • 2
    Looks fine, except that you need to set the member before you can read it. That's just a sequence-of-events problem Commented Feb 9, 2018 at 17:12

1 Answer 1

1

You can't access myVar from within myChild.__init__ because you're creating the myChild instance before you create the self.myVar attribute. Try switching the order of those lines.

class myParent():
    def __init__(self, whatever):
        self.myVar = whatever
        self.child = myChild(parent_ = self)
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.