【发布时间】:2011-09-01 15:06:53
【问题描述】:
如何从b.func()(即A 的实例的self)中内省A 的实例:
class A():
def go(self):
b=B()
b.func()
class B():
def func(self):
# Introspect to find the calling A instance here
【问题讨论】:
-
请注意,在 Python 2.x 中,最好从
object继承而不是什么都不继承,这样您就可以使用 新型类。 -
没有可移植的方法来做到这一点(在 CPython 中做到这一点的唯一方法是抓取父框架并检查它,但并非所有 Python 实现都会公开这些数据)
标签: python introspection