关于Python列表可变性的技术疑问:是否仅在函数内通过索引修改时列表才具备可变性?
Great question! Let's unpack what's going on here—your confusion comes from mixing up variable assignment and modifying a mutable object itself, not whether lists are only mutable when using indexes.
First, let's confirm: Lists are always mutable in Python. Mutability means you can change the contents of the object itself without creating a new object. The difference between your two examples has nothing to do with indexes specifically—it's about what you're doing to the variable x inside the function.
Let's break down Example 1
x = [1] print(id(x)) def test(): x = [2] # This is a NEW local variable, not the global x test() print(x) # Output: [1] print(id(x)) # Same ID as before
Here's the key: When you write x = [2] inside the function, you're not modifying the original list that the global x points to. Instead, you're creating a new local variable named x (only visible inside the function) that points to a brand new list [2]. The global x never changes—it still points to the original [1] list.
If you wanted to reassign the global x inside the function, you'd need to use the global keyword:
x = [1] def test(): global x x = [2] # Now this modifies the global x variable test() print(x) # Output: [2]
But even here, you're not mutating the original list—you're making the global x point to a completely new list object.
Now Example 2
x = [1] print(x) print(id(x)) def test(): x[0] = 2 # This modifies the EXISTING list object test() print(x) # Output: [2] print(id(x)) # Same ID as before
In this case, you're not creating a new variable x inside the function. Instead, you're accessing the list object that x points to (the global one, since you didn't reassign x locally) and modifying its contents directly. Since lists are mutable, this change affects the original object—so the global x still points to the same list, but the list's internal value has changed.
It's not just indexes—any operation that modifies the list object works
Lists are mutable no matter how you modify them, as long as you're changing the object itself, not reassigning the variable. For example:
x = [1] def test(): x.append(2) # Modifies the original list test() print(x) # Output: [1, 2] x = [1, 2] def test(): x.extend([3,4]) # Also modifies the original list test() print(x) # Output: [1,2,3,4] x = [1,2,3] def test(): x.pop() # Still modifies the original list test() print(x) # Output: [1,2]
All these operations change the original list object, just like the index assignment did—no new list is created, so the global x reflects the changes.
To sum up
- Lists are always mutable in Python—their mutability doesn't depend on using indexes or being inside a function.
- The difference in your examples is:
- In Example 1: You created a new local variable
xinside the function, leaving the globalxand its list untouched. - In Example 2: You modified the existing list object that the global
xpoints to, so the changes are visible outside the function.
- In Example 1: You created a new local variable
内容的提问来源于stack exchange,提问作者CaptainAble2500

