获取属性的所有者实例



当此属性传递给没有其实例的函数时,我想获取属性的实例句柄。为了更清楚,请参阅下面的示例代码:

class aClass():
def __init__(self):
self.anInstanceAttribute = 'ok'
def aFunction(anInstanceAttribute):
print(anInstanceAttribute)
#how to get the instance handle ('the self') of the anInstanceAttribute?
a = aClass()
aFunction(a.anInstanceAttribute)

如果没有内省/框架黑客,这是不可能的。

aFunction(a.anInstanceAttribute)

在调用函数之前,将完全计算函数参数。 因此,该函数"ok"接收字符串对象,并且对实例a一无所知。 如果您希望函数了解有关实例的某些信息,请改为传入a

最新更新