2

I can change an element of a list through the following code:

def myFunction(x):
    x[0] = 2
    
myList = [4,5,6]
myFunction(myList)
print(myList)  # ==> outputs [2,5,6] (myList[0] is changed to '2' through the function). 

But I don't understand why I cannot change a variable using the same code:

def myFunction(x):
    x = 2
    
myNumber = 6
myFunction(myNumber)
print(myNumber)  # ==> outputs 6 (myNumber is not changed to '2' through the function).
Freddy Mcloughlan
  • 4,129
  • 1
  • 13
  • 29
Orchard
  • 29
  • 2
  • 5
    Required reading: [Python Names and Values](https://nedbatchelder.com/text/names1.html) and [Facts and myths about Python names and values](https://nedbatchelder.com/text/names.html). – jarmod Mar 29 '22 at 22:32
  • 1
    Related, possible duplicate: https://stackoverflow.com/q/986006/4014959 – PM 2Ring Mar 29 '22 at 23:14
  • 1
    Does that ↑, answer your question? – wwii Mar 30 '22 at 00:57

2 Answers2

2
def myFunction(x):
    x = 2
    return x

myNumber = 6
myNumber = myFunction(myNumber)
print(myNumber)

You can't change the variable the way you wrote. Instead you can return a value and assign it to a variable.

1

This method of changing variables globally is not the best way to solve the problem. However; you can do this:

def myFunction(x):
    globals()[x] = 2


myNumber = 6
# Must pass variable name as literal string
myFunction("myNumber")
print(myNumber)

Getting a variable as a string is discussed here


I suggest you do something like this (to avoid using globals):

def myFunction():
    return 2


myNumber = 6
myNumber = myFunction()
print(myNumber)

More specifically, you would do this for lists:

This is the unconventional global method (not preferred)

def myFunction(x):
    globals()[x][1] = 2


myNumbers = [0, 0, 0]
# Must pass variable name as literal string
myFunction("myNumbers")
print(myNumbers)

And the preferred method:

def myFunction():
    return 2


myNumbers = [0, 0, 0]
myNumbers[1] = myFunction()
print(myNumbers)
Freddy Mcloughlan
  • 4,129
  • 1
  • 13
  • 29
  • But how am I able to change an element of a list in such a way that doesn't work when reassigning a variable? – Orchard Mar 29 '22 at 22:48
  • Like your first piece of code in the question? – Freddy Mcloughlan Mar 29 '22 at 22:49
  • Yes, like the first piece of code in the OP. – Orchard Mar 29 '22 at 22:53
  • I'll edit the post – Freddy Mcloughlan Mar 29 '22 at 22:59
  • I can already reassign an element of a list using the first piece of code in the OP. My question is: This code doesn't work with reassigning variables (and I can understand why that is so). But this code works when reassigning an element of a list. Why does it work with an element of a list, but not with a variable, for example with a list. – Orchard Mar 29 '22 at 23:13
  • 1
    That answer is a bit of a read. As suggested in the comments, you may find @jarmod's links answer your question – Freddy Mcloughlan Mar 29 '22 at 23:20
  • 1
    See [Why can a function modify some arguments as perceived by the caller, but not others?](https://stackoverflow.com/questions/575196/why-can-a-function-modify-some-arguments-as-perceived-by-the-caller-but-not-oth). – jarmod Mar 30 '22 at 13:52