I have a function which repeatedly indexes into a dictionary with the same key. Is it possible to take a reference to the item in question?
def my_function(self, event):
self.__observers[event] ... #this is get is performed multiple times
observer_ref = self.__observers[event] #can I make this a reference to the value?
Update:
The goal is to not take a copy of what is at self.__observers[event]. I am looking for behaviour akin to C++, e.g.
int x = 1
int& y = x #reference to y, not a copy
When you assign a value to a name (variable) in Python, the variable is just a reference to the value.
So for example say
self.__observers[event]starts as an empty list, the following code would work fine:However, as pointed out by BrenBarn, if you were to then assign
observer_refa new value, it would not modifyself.__observers[event]at all.