Skip to content Skip to sidebar Skip to footer

How To Reference Creator Object In Python?

I have a question. There are two classes: A and B. A creates object of type B. So from A, it is easy to access methods of B, but how can I access methods of object A from object B?

Solution 1:

Pass the creator as an argument to the constructor:

classB(object):def__init__(self, creator):
        self._creator = creator   # or do something else with it

Use as:

classA(object):defsomemethod(self):
        b = B(self)

Solution 2:

There's no way to do this automatically. You'll just have to give each B a reference to A when you create it, something like b_instance.parent = a_instance.

Post a Comment for "How To Reference Creator Object In Python?"