def call(nums): nums[:] = [x x in nums if x != 4] numbers = [4, 5] print(numbers) call(numbers) print(numbers)
the output above code is:
[4, 5] [5]
but if remove "[:]", output becomes following:
[4, 5] [4, 5]
i know [:]
makes copy of full list, why function argument modified in 1 case , not in other?
as suspected issue hiding in slicer in line:
nums[:] = [x x in nums if x != 4]
python "pass value" means you're not passing pointer numbers
function, copy of it. re-assigning value copy of reference not change original reference.
but, when use slicer, you're accessing object behind reference , changing directly why see side effect after exit function - when using "slice".
Comments
Post a Comment