6

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)

Sнаđошƒаӽ
  • 16,753
  • 12
  • 73
  • 90
random guy
  • 2,225
  • 6
  • 24
  • 24
  • 1
    duplicate? http://stackoverflow.com/questions/1145722/simulating-pointers-in-python – jbochi Jan 14 '10 at 15:30
  • 1
    We call this an "alias". There are two names for `a`: `a` and `d['a']`. This is usually considered a bad thing because it's not obvious why something changes. Why do you want to do this? What's the point of creating intentionally obscure code? – S.Lott Jan 14 '10 at 15:58
  • See this question: http://stackoverflow.com/questions/1145722/simulating-pointers-in-python – Ben Gartner Jan 14 '10 at 15:24
  • @S.Lott, It might be the case where you have multiple large data structures and they point to other large data structure data and you need to modify them in a thread and show in the UI for example. – ArmenB Nov 29 '16 at 00:58

5 Answers5

4

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
danben
  • 80,905
  • 18
  • 123
  • 145
2

The standard solution is to just use a list; it's the easiest mutable structure.

a = [1]
d = {'a': a}
a[0] = 2
print d['a'][0]
Johann Hibschman
  • 1,997
  • 1
  • 16
  • 17
  • Not a graceful solution, but better than the others and simple. Weird this is not just part of the language in my opinion. I could write an easier to read answer in C than I could in Python for this case. – Samuel Dec 17 '14 at 17:44
1

This is because 1 is a immutable datatype in python, i.e. you can't change the value of it.

To make it work like a pointer, you need a mutable datatype as storage, which you can do yourself with a class definition

class Mutable(object):
    pass

a = Mutable()
a.value = 1

d = {'a':a}
a.value = 3

d['a'].value equals 3 at this point.

If you really want, you can overload operators etc. so that you get the same semantics as for normal integers, but I'd suggest you instead take a look at some functional programming patterns to see why immutable types are nice.

Joakim Lundborg
  • 10,920
  • 6
  • 32
  • 39
0

If you're working in a class you could do something like this:

class DRefsA(object):
    a = 4

    @property
    def d(self):
        return self.a

    @d.setter
    def d(self, value):
        self.a = value
jcdyer
  • 18,616
  • 5
  • 42
  • 49
0

a bit late to the party but heres a solution that works with any data type with lamda

a = 1
d = {'a': lambda  : a}
print(d['a']()) #output 1
a = 2
print(d['a']()) #output 2
coder
  • 700
  • 8
  • 12