If I have this:
class A:
def callFunction(self, obj):
obj.otherFunction()
class B:
def callFunction(self, obj):
obj.otherFunction()
class C:
def otherFunction(self):
# here I wan't to have acces to the instance of A or B who call me.
...
# in main or other object (not matter where)
a = A()
b = B()
c = C()
a.callFunction(c) # How 'c' know that is called by an instance of A...
b.callFunction(c) # ... or B
Despite the design or other issues, this is only a question of an inquiring mind.
Note: This has to be done without changing otherFunction
signature
Here is a quick hack, get the stack and from last frame get locals to access self
output:
Examine the stack with the inspect module with
inspect.stack()
. You can then get the instance from each element in the list withf_locals['self']
If this is for debugging purposes you can use inspect.currentframe():
Here is the output: