这实际上取决于您所说的“参考”是什么意思。...而且它还取决于您将如何处理参考。例如。
class Test(object):
def __init__(self):
self.__observers = {'event1': {'test':'this'}, 'event2': {'test':'that'}}
def get_observers(self):
return self.__observers
def my_function(self, event):
observer_ref = self.__observers[event]
return observer_ref
if __name__ == '__main__':
t = Test()
my_event = 'event1'
print('From class: {}'.format(t.get_observers()[my_event]))
my_ref = t.my_function(my_event)
print('From my_function: {}'.format(my_ref))
my_ref['something'] = 'else'
print('From my_function after addition: {}'.format(my_ref))
print('From class after addition: {}'.format(t.get_observers()[my_event]))
my_ref = 'something else entirely'
print('From my_function after re-assignment: {}'.format(my_ref))
print('From class after re-assignment: {}'.format(t.get_observers()[my_event]))
结果是 :
From class: {'test': 'this'}
From my_function: {'test': 'this'}
From my_function after addition: {'test': 'this', 'something': 'else'}
From class after addition: {'test': 'this', 'something': 'else'}
From my_function after re-assignment: something else entirely
From class after re-assignment: {'test': 'this', 'something': 'else'}
基本上,如果您reference
是某种对象,您可以使用该引用来操作该对象。您只是不能将引用的名称重新分配给其他东西,并将其反映在您从中提取引用的原始对象中。