【发布时间】:2019-04-13 16:25:56
【问题描述】:
我有一个很奇怪的问题。
我需要返回一个类对象来调用一个应该返回调用它的类对象的函数。我知道我知道。把它想象成一个人为的练习,虽然对我来说这是一个非常现实的需要。
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)
是否可以在baz() 中编写任何返回调用它的对象的内容?
编辑:
回答cmets:
我尝试使用检查,但没有成功,我什至查看了整个堆栈,但找不到与 new_foo 对象匹配的条目:
new_foo 打印出来时是这样的:<__main__.Foo object at 0x0000029AAFC4C780>
当我打印出整个堆栈时,在其中找不到条目:
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)]
所以我不想让它返回Foo 的新实例,但实际上与new_foo 完全相同。
【问题讨论】:
-
看看stackoverflow.com/questions/900392/… 是否有帮助。如果您遇到任何问题,请在评论中告诉我。
-
当您致电
new_foo.bar(baz)时,您希望返回什么。是新的Foo实例吗? -
@AbdulNiyasPM @hygull 与
new_foo相同的实例我已编辑问题以进行澄清 -
@Legit Stack 如果是这种情况,你不能用
self调用函数(在这种情况下为baz)吗?(即return func(self) -
@AbdulNiyasPM 哦,是的,我认为修改为
bar会起作用