-1

In the following code, the function modify_list want to modify b, but it failed, the print result is [1,2,3]. why hasn't the list a changed?

def modify_list(b):
    b = [1,2]
a = [1,2,3]
modify_list(a)
print(a)
liangsun
  • 37
  • 5
  • 1
    Assignments don't change the thing that was previously assigned. Read https://nedbatchelder.com/text/names.html – chepner Feb 19 '20 at 15:08
  • Python doesn't have call-by-reference semantics. – chepner Feb 19 '20 at 15:08
  • Python is a pass-by-value language. https://en.wikipedia.org/wiki/Evaluation_strategy#Call_by_value – Felix Kling Feb 19 '20 at 15:08
  • @FelixKling If python is a pass-by-value language, why b[:] = [1,2] can work? – liangsun Feb 19 '20 at 15:16
  • The value can be a reference. That's different from pass-by-reference (the code in your question) though. I know that the fact that "reference" is used everywhere is not helping... – Felix Kling Feb 19 '20 at 15:21
  • @liangsun Because then you are telling Python to bascially empty the original one and push the values again. – epsilonmajorquezero Feb 19 '20 at 15:21
  • Python has a weird rule for passing arguments. If you pass simple variables like numbers, they're by value. If you pass objects, they're by reference unless you declare the same value again, then python makes a copy of it. So as long as you edit the object, it's by reference. – anishtain4 Feb 19 '20 at 15:28
  • @anishtain4 That's right, here, List is passed by reference, but the reference is passed by value, so If you want rebind, you can't change the outer variable(a). – liangsun Feb 19 '20 at 15:30
  • 3
    Does this answer your question? [Python functions call by reference](https://stackoverflow.com/questions/13299427/python-functions-call-by-reference) – anishtain4 Feb 19 '20 at 15:34

2 Answers2

3

you are declaring other local variable b, if you want to mutate you can do:

b[:] = [1, 2]

or even better you can return your desired value for list a

if you want to change your list a value you can assign the desired value:

a = [1, 2]
kederrac
  • 16,819
  • 6
  • 32
  • 55
  • In c++, it can pass value or reference, python only can pass the value? – liangsun Feb 19 '20 at 15:14
  • you may have a look over: https://stackoverflow.com/questions/13299427/python-functions-call-by-reference – kederrac Feb 19 '20 at 15:19
  • I wouldn't use the syntax `list[:] = ` as it does not discriminate between global and local variables. `list[:]` is the equivalent of using [`operator.setitem()`](https://docs.python.org/3/library/operator.html#mapping-operators-to-functions) and can lead to some nasty side effects in the larger context of your program. It is best to return a modified copy of `a` and assign `a` to this modified copy. – felipe Feb 19 '20 at 15:39
  • I have been mention if the OP wants to mutate ... – kederrac Feb 19 '20 at 15:42
3

If you want something like this to work you can do:

def modify_list(b):
    return [1,2]
a = [1,2,3]
a = modify_list(a)
print(a)

Or

def modify_list(b):
    b[:] = [1,2]
a = [1,2,3]
modify_list(a)
print(a)