Could someone please explain the following result in Python?
When running the following snippet of code, Python throws an error, saying that the variable x
was referenced before assignment:
x = 1
def increase_x():
x += 1
increase_x()
The solution, of course, would be to include the line global x
after the function declaration for increase_x
.
However, when running this next snippet of code, there is no error, and the result is what you expect:
x = [2, -1, 4]
def increase_x_elements():
for k in range(len(x)):
x[k] += 1
increase_x_elements()
Is this because integers are primitives in Python (rather than objects) and so x
in the first snippet is a primitive stored in memory while x
in the second snippet references a pointer to a list object?