1

After executing the following code,

my_list = []
v = 1
my_list.append(v)
v = 2
print(my_list)

the result is

[1]

I think this happens because "v" is appended by value, not by reference. Is there anyway to pass it by reference on appending, so that result can be 2?

Younghoon Jeong
  • 753
  • 1
  • 5
  • 7
  • 3
    Python does not support references to variables. You can only have references to objects. `v = 2` rebinds `v` to refer to a new object; this will never affect the list's reference. – user2357112 Jul 11 '17 at 22:22
  • 3
    You will need to use a different reference type than integer for it to be mutable. Integers are immutable, and for that, they are appended by value. It would need to be an object, like a list if you want a simple python object, or your own predefined object. – Davy M Jul 11 '17 at 22:22

0 Answers0