Python equivalent of pointers

pointers, python, reference

Solution

What about a mutable data structure?

>>> a = mutable_structure(1)
>>> d = {'a':a}
>>> d['a']
1
>>> a.setValue(2)
>>> d['a']
2

An implementation might look like

class mutable_structure:
  def __init__(self, val):
    self.val = val

  def __repr__(self):
    return self.val

Problem

In python everything works by reference: ``` >>> a = 1 >>> d = {'a':a} >>> d['a'] 1 >>> a = 2 >>> d['a'] 1 ``` I want something like this ``` >>> a = 1 >>> d = {'a':magical pointer to a} >>> d['a'] 1 >>> a = 2 >>> d['a'] 2 ``` What would you substitute for magical pointer to a so that python would output what I want. I would appreciate general solutions (not just for the above dictionary example with independent variables, but something that would work for other collections and class/instance variables)

Original source

Related problems