Learn Web Development with Python
上QQ阅读APP看书,第一时间看更新

Assignment to argument names doesn't affect the caller

This is something that can be tricky to understand at first, so let's look at an example:

# key.points.assignment.py
x = 3
def func(x):
x = 7 # defining a local x, not changing the global one
func(x)
print(x) # prints: 3

In the preceding code, when the x = 7 line is executed, within the local scope of the func function, the name, x, is pointed to an integer with a value of 7, leaving the global x unaltered.