使用Python,是否有任何方法來存儲對引用的引用,以便我可以更改該引用在另一個上下文中引用的內容?例如,假設我有下面的類:在Python中存儲對引用的引用?
class Foo:
def __init__(self):
self.standalone = 3
self.lst = [4, 5, 6]
我想創造一些類似以下內容:
class Reassigner:
def __init__(self, target):
self.target = target
def reassign(self, value):
# not sure what to do here, but reassigns the reference given by target to value
使得下面的代碼
f = Foo()
rStandalone = Reassigner(f.standalone) # presumably this syntax might change
rIndex = Reassigner(f.lst[1])
rStandalone.reassign(7)
rIndex.reassign(9)
會導致f.standalone
等於7
和f.lst
等於[4, 9, 6]
。
本質上,這將是一個模擬的指針到指針。
我認爲引用的重點在於您不應該能夠這樣做。 – 2013-05-09 20:16:52
你打算如何使用它? – FogleBird 2013-05-09 20:19:44
如果你想要C++,你知道在哪裏可以找到它... – pydsigner 2013-05-09 20:23:05