[英]Get Caller Class In Python
I've a class calling another class's function, like this: 我有一个类调用另一个类的函数,如下所示:
self.pool.get('some_value')
How do I get the caller class inside the get function ? 如何在get函数中获取调用者类?
thanks before, 谢谢之前,
You can do this via inspect
. 你可以通过
inspect
来做到这一点。 The contents of the get()
function can be: get()
函数的内容可以是:
import inspect
myCaller = inspect.currentframe().f_back.f_locals['self']
After that, you have the caller's instance refered to by myCaller
. 之后,
myCaller
调用调用者的实例。
Disclaimer : this is not a good practice, because it ruins the whole point of encapsulation. 免责声明 :这不是一个好习惯,因为它破坏了整个封装点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.