7

Code:

>>> a = 1
>>> b = 2
>>> l = [a, b]
>>> l[1] = 4
>>> l
[1, 4]
>>> l[1]
4
>>> b
2

What I want to instead see happen is that when I set l[1] equal to 4, that the variable b is changed to 4.

I'm guessing that when dealing with primitives, they are copied by value, not by reference. Often I see people having problems with objects and needing to understand deep copies and such. I basically want the opposite. I want to be able to store a reference to the primitive in the list, then be able to assign new values to that variable either by using its actual variable name b or its reference in the list l[1].

Is this possible?

fdmillion
  • 4,823
  • 7
  • 45
  • 82
  • You have `l` with a reference to `b`, not the other way around. – Rushy Panchal May 23 '13 at 01:47
  • this question is very abstract. Are you trying to learn more about how python works (in comparison with other languages you might know) or are you trying to use this as the solution to something? what problem are you trying to solve? – SingleNegationElimination May 23 '13 at 03:05

5 Answers5

7

There are no 'primitives' in Python. Everything is an object, even numbers. Numbers in Python are immutable objects. So, to have a reference to a number such that 'changes' to the 'number' are 'seen' through multiple references, the reference must be through e.g. a single element list or an object with one property.

(This works because lists and objects are mutable and a change to what number they hold is seen through all references to it)

e.g.

>>> a = [1]
>>> b = a
>>> a
[1]
>>> b
[1]
>>> a[0] = 2
>>> a
[2]
>>> b
[2]
Patashu
  • 21,443
  • 3
  • 45
  • 53
5

You can't really do that in Python, but you can come close by making the variables a and b refer to mutable container objects instead of immutable numbers:

>>> a = [1]
>>> b = [2]
>>> lst = [a, b]
>>> lst
[[1], [2]]
>>> lst[1][0] = 4  # changes contents of second mutable container in lst
>>> lst
[[1], [4]]
>>> a
[1]
>>> b
[4]
martineau
  • 119,623
  • 25
  • 170
  • 301
  • This answer and the one above it basically say the same thing in different ways...thanks for clearing up the immutable vs primitive confusion for me! – fdmillion Jun 11 '13 at 08:16
1

I don't think this is possible:

>>> lst = [1, 2]
>>> a = lst[1] # value is copied, not the reference
>>> a
2
>>> lst[1] = 3
>>> lst
[1, 3] # list is changed
>>> a # value is not changed
2

a refers to the original value of lst[1], but does not directly refer to it.

Rushy Panchal
  • 16,979
  • 16
  • 61
  • 94
0

There were a relevant discussion earlier: Storing elements of one list, in another list - by reference - in Python?

According to @mgilson, when doing l[1] = 4, it simply replaces the reference, rather than trying to mutate the object. Nevertheless, objects of type int are immutable anyway.

Community
  • 1
  • 1
chenaren
  • 2,090
  • 1
  • 19
  • 24
0

Think of l[0] as a name referring to an object a, and a as a name that referring to an integer.

Integers are immutable, you can make names refer to different integers, but integers themselves can't be changed.

satoru
  • 31,822
  • 31
  • 91
  • 141