Okay python gurus, this one has me stumped. Is it possible to introspect and obtain the instance of a class that assigned a class?
I have a simple sample that demonstrates what I'm looking for, but doesn't make any sense why you would do it.
class One(object):
def print_parent_hw(self):
?????????
?????????.print_hw()
class Two(object):
def __init__(self):
self.cls = One()
def print_hw(self):
print "Hello World from One"
def have_one_print_two(self):
self.cls.printparent_hw()
a = Two()
a.have_one_print_two()
I could do it by rewriting some code, but this situation could be cleaner and quicker (even though the sample doesn't make much logical sense). I realize I could do it like this, but it doesn't seem right, and you end up with a circular attribute (i.e. a.cls.parent.cls.parent.cls.parent.cls.......)
class One(object):
def __init__(self, parent):
self.parent = parent
def print_parent_hw(self):
self.parent.print_hw()
class Two(object):
def __init__(self):
self.cls = One(self)
def print_hw(self):
print "Hello World from One"
def have_one_print_two(self):
self.cls.print_parent_hw()
a = Two()
a.have_one_print_two()