- Learn Web Development with Python
- Fabrizio Romano Gaston C. Hillar Arun Ravindran
- 93字
- 2021-06-10 18:26:11
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.