提问人:rbewoor 提问时间:1/29/2021 更新时间:1/29/2021 访问量:484
为什么列表变量有时不受函数更改的影响,因为我认为 python3 可以通过引用列表变量进行传递?
Why is a list variable sometimes not impacted by changes in function as I thought python3 works on pass by reference with list variables?
问:
对于python3,我最初需要从列表中提取奇数和偶数位置并将其分配给新列表,然后清除原始列表。我以为列表受到通过“按引用传递”的函数调用的影响。测试一些场景,它有时会起作用。有人可以解释一下 python3 在这里究竟是如何工作的吗?
情况 1:空列表按预期填充字符串。
def func1(_in):
_in.append('abc')
mylist = list()
print(f"Before:\nmylist = {mylist}")
func1(mylist)
print(f"After:\nmylist = {mylist}")
输出情况 1:
Before:
mylist = []
After:
mylist = ['abc']
情况 2:中间列表元素按预期替换为字符串。
def func2(_in):
_in[1] = 'abc'
mylist = list(range(3))
print(f"Before:\nmylist = {mylist}")
func2(mylist)
print(f"After:\nmylist = {mylist}")
输出情况 2:
Before:
mylist = [0, 1, 2]
After:
mylist = [0, 'abc', 2]
案例3:为什么函数调用后列表不为空?
def func3(_in):
_in = list()
mylist = list(range(3))
print(f"Before:\nmylist = {mylist}")
func3(mylist)
print(f"After:\nmylist = {mylist}")
输出情况 3:
Before:
mylist = [0, 1, 2]
After:
mylist = [0, 1, 2]
情况 4:完全按预期工作,但请注意,我已经从函数中返回了所有三个列表。
def func4_with_ret(_src, _dest1, _dest2):
_dest1 = [val for val in _src[0:len(_src):2]]
_dest2 = [val for val in _src[1:len(_src):2]]
_src = list()
return _src, _dest1, _dest2
source = list(range(6))
evens, odds = list(), list()
print(f"Before function call:\nsource = {source}\nevens = {evens}\nodds = {odds}")
source, evens, odds = func4_with_ret(source, evens, odds)
print(f"\nAfter function call:\nsource = {source}\nevens = {evens}\nodds = {odds}")
输出情况 4:
Before function call:
source = [0, 1, 2, 3, 4, 5]
evens = []
odds = []
After function call:
source = []
evens = [0, 2, 4]
odds = [1, 3, 5]
案例 5:如果我不显式地从函数调用返回,为什么对函数外部的变量没有影响?
def func5_no_ret(_src, _dest1, _dest2):
_dest1 = [val for val in _src[0:len(_src):2]]
_dest2 = [val for val in _src[1:len(_src):2]]
_src = list()
source = list(range(6))
evens, odds = list(), list()
print(f"Before function call:\nsource = {source}\nevens = {evens}\nodds = {odds}")
func5_no_ret(source, evens, odds)
print(f"\nAfter function call:\nsource = {source}\nevens = {evens}\nodds = {odds}")
输出情况 5:
Before function call:
source = [0, 1, 2, 3, 4, 5]
evens = []
odds = []
After function call:
source = [0, 1, 2, 3, 4, 5]
evens = []
odds = []
谢谢。
答:
您的最终问题是将(就地)突变与重新结合(也称为“重新分配”)混淆。
如果更改在函数外部不可见,则可以在函数内部重新显示名称。当您执行以下操作时:
name = val
过去是什么并不重要;它反弹到 ,并且对旧对象的引用被扔掉了。当它是最后一个引用时,这会导致对象被清理;在本例中,用于为对象增别的参数也绑定到调用方中的名称,但在重新绑定后,该别名关联将丢失。name
val
除了 C/C++ 的人:重新绑定就像分配给指针变量,例如 (初始绑定),然后是 (重新绑定),其中 和 本身都指向 。当赋值发生时,过去指向的事物与现在指向新事物的事物无关紧要,并且紧随其后(突变,而不是重新绑定)只会影响指向的任何内容,而目标保持不变。int *px = pfoo;
px = pbar;
pfoo
pbar
int
px = pbar;
px
pfoo
*px = 1;
pbar
pfoo
相比之下,突变不会破坏混叠关联,因此:
name[1] = val
确实会重新绑定自己,但不会重新绑定;它继续像以前一样引用相同的对象,它只是在原地改变该对象,保持所有别名完好无损(因此所有别名相同对象的名称都会看到更改的结果)。name[1]
name
对于您的具体情况,您可以通过更改为切片分配/删除或其他形式的就地突变,将“损坏”的函数从重新绑定更改为别名,例如:
def func3(_in):
# _in = list() BAD, rebinds
_in.clear() # Good, method mutates in place
del _in[:] # Good, equivalent to clear
_in[:] = list() # Acceptable; needlessly creates empty list, but closest to original
# code, and has same effect
def func5_no_ret(_src, _dest1, _dest2):
# BAD, all rebinding to new lists, not changing contents of original lists
#_dest1 = [val for val in _src[0:len(_src):2]]
#_dest2 = [val for val in _src[1:len(_src):2]]
#_src = list()
# Acceptable (you should just use multiple return values, not modify caller arguments)
# this isn't C where multiple returns are a PITA
_dest1[:] = _src[::2] # Removed slice components where defaults equivalent
_dest2[:] = _src[1::2] # and dropped pointless listcomp; if _src might not be a list
# list(_src[::2]) is still better than no-op listcomp
_src.clear()
# Best (though clearing _src is still weird)
retval = _src[::2], _src[1::2]
_src.clear()
return retval
# Perhaps overly clever to avoid named temporary:
try:
return _src[::2], _src[1::2]
finally:
_src.clear()
评论
_in = list()
_in
_in
_in.clear()
_dest1
_dest1.extend([val for val in _src[0:len(_src):2]])
list
_in
_in
_in = some_global
_in
_in.append
_in[i] = x
_in = list()