python反射找到引用对象

pro*_*ach -1 python reflection

如果我有以下代码

 class One:
   scope = 'first_scope'

 class Two:
   scope = 'second_scope'
   contained_object = One()
Run Code Online (Sandbox Code Playgroud)

我是否有可能 contained_object通过反思来确定它和引用它的对象是否具有相同的含义scope

谢谢

编辑:道歉,如果问题不清楚,我不太确定python术语如何问.我设计了一种样本

一个例子可能是

 def sample(input):
     #code in here to find out if input.scope
     # matches a.scope without having a reference to it

 a = Two()
 a.scope = 'first scope'
 a.contained_object.scope = 'will not match'
 sample(a.contained_object)
Run Code Online (Sandbox Code Playgroud)

Mar*_*cin 5

你的问题不清楚.如果你的意思是你在变量contained_objectscope存在的上下文中,并且contained_object拥有一个具有该属性的对象scope,那么当然可以.

如果你的意思是你只是有一个类型的对象One,并且你想找到它的引用,你可以使用gc.get_referrers(http://docs.python.org/library/gc.html#gc.get_referrers)