I have a very strange problem.
I need to return a class object to call a function that is supposed to return the class object that called it. I know, I know. Just think of it as a contrived exercise, though for me it is a very real need.
def baz():
# return the object instance that calls me.
class Foo():
def bar(self, func):
return func() # should return the Foo object but how???
new_foo = Foo()
new_foo_instance = new_foo.bar(baz)
is it possible to write anything in baz()
that will return the object that called it?
EDIT:
to answer the comments:
I have tried to use inspect, but with no success, I even looked at the entire stack but I cannot find an entry that matches the new_foo
object:
new_foo
looks like this when I printed it out: <__main__.Foo object at 0x0000029AAFC4C780>
when I printed out the entire stack that entry was not found within it:
def baz():
print(inspect.stack())
return inspect.stack() #[1][3]
>>> [FrameInfo(frame=<frame object at 0x0000029AADB49648>, filename='return_caller.py', lineno=5, function='baz', code_context=[' print(inspect.stack())\n'], index=0), FrameInfo(frame=<frame object at 0x0000029AAD8F0DE8>, filename='return_caller.py', lineno=11, function='bar', code_context=[' return func() # should return the Foo object but how???\n'], index=0), FrameInfo(frame=<frame object at 0x0000029AAD8AC588>, filename='return_caller.py', lineno=19, function='<module>', code_context=['new_foo_instance = new_foo.bar(baz)\n'], index=0)]
So I am not trying to get it to return a new instance of Foo
, but actually the exact same instance as new_foo
.