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' #
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)
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.