从函数返回列表,覆盖列表变量,比修改函数中的列表更好。

时间:2018-12-09 03:48:55

标签: python

我了解Python中可变的和不变的概念。但是,我发现有时在某些情况下处理它们会造成混乱。让我们在这里说明一些例子。我还打印了id个这些Python对象:

a = [1,2,3]
b = [1,2,3]
c = [1,2,3]

def fun(param_a, param_b, param_c):
    print(f'  id(param_a)={id(param_a)}  param_a = {param_a}')
    print(f'  id(param_b)={id(param_b)}  param_b = {param_b}')
    print(f'  id(param_c)={id(param_c)}  param_c = {param_c}')

    param_a.append(999)  # It will change a
    local_a = param_a    # Make a local alias to param_a
    local_a.append(12345)
    param_b = [3,3,3]    # Python creates a new object
    param_c.clear()      # Python will change c
    param_c.append(999)

    print(f'  id(param_a)={id(param_a)}  param_a = {param_a}')
    print(f'  id(param_b)={id(param_b)}  param_b = {param_b}')
    print(f'  id(param_c)={id(param_c)}  param_c = {param_c}')

print(f'id(a)={id(a)}  a = {a}')
print(f'id(b)={id(b)}  a = {b}')
print(f'id(c)={id(c)}  a = {c}')
print('Calling func(a,b,c)')
fun(a, b, c)
print('Return from func(a,b,c)')
print(f'id(a)={id(a)}  a = {a}')
print(f'id(b)={id(b)}  a = {b}')
print(f'id(c)={id(c)}  a = {c}')

输出:

id(a)=2552952620744  a = [1, 2, 3]
id(b)=2552952582536  a = [1, 2, 3]
id(c)=2552952620424  a = [1, 2, 3]
Calling func(a,b,c)
  id(param_a)=2552952620744  param_a = [1, 2, 3]
  id(param_b)=2552952582536  param_b = [1, 2, 3]
  id(param_c)=2552952620424  param_c = [1, 2, 3]
  id(param_a)=2552952620744  param_a = [1, 2, 3, 999, 12345]
  id(param_b)=2552952619272  param_b = [3, 3, 3]
  id(param_c)=2552952620424  param_c = [999]
Return from func(a,b,c)
id(a)=2552952620744  a = [1, 2, 3, 999, 12345]
id(b)=2552952582536  a = [1, 2, 3]
id(c)=2552952620424  a = [999]

一种更简单的方法是返回列表,覆盖全局列表。唯一的问题是性能和内存要求。

a = [1,2,3]
b = [1,2,3]
c = [1,2,3]

def fun(param_a, param_b, param_c):
    print(f'  id(param_a)={id(param_a)}  param_a = {param_a}')
    print(f'  id(param_b)={id(param_b)}  param_b = {param_b}')
    print(f'  id(param_c)={id(param_c)}  param_c = {param_c}')

    param_a.append(999)  # It will change a
    local_a = param_a    # Make a local alias to param_a
    local_a.append(12345)
    param_b = [3,3,3]    # Python creates a new object
    param_c = [999]      # Python creates a new object

    print(f'  id(param_a)={id(param_a)}  param_a = {param_a}')
    print(f'  id(param_b)={id(param_b)}  param_b = {param_b}')
    print(f'  id(param_c)={id(param_c)}  param_c = {param_c}')
    return param_a, param_b, param_c

print(f'id(a)={id(a)}  a = {a}')
print(f'id(b)={id(b)}  a = {b}')
print(f'id(c)={id(c)}  a = {c}')
print('Calling func(a,b,c)')
a, b, c = fun(a, b, c)
print('Return from func(a,b,c)')
print(f'id(a)={id(a)}  a = {a}')
print(f'id(b)={id(b)}  a = {b}')
print(f'id(c)={id(c)}  a = {c}')

输出:

id(a)=2552952621192  a = [1, 2, 3]
id(b)=2552952619464  a = [1, 2, 3]
id(c)=2552952620744  a = [1, 2, 3]
Calling func(a,b,c)
  id(param_a)=2552952621192  param_a = [1, 2, 3]
  id(param_b)=2552952619464  param_b = [1, 2, 3]
  id(param_c)=2552952620744  param_c = [1, 2, 3]
  id(param_a)=2552952621192  param_a = [1, 2, 3, 999, 12345]
  id(param_b)=2552952620424  param_b = [3, 3, 3]
  id(param_c)=2552952621256  param_c = [999]
Return from func(a,b,c)
id(a)=2552952621192  a = [1, 2, 3, 999, 12345]
id(b)=2552952620424  a = [3, 3, 3]
id(c)=2552952621256  a = [999]

我也无法使用copy.deepcopy()将参数传递给函数。

是否有更好的方法来修改传递的参数(可能不是简单的列表,集合或字典)​​?

0 个答案:

没有答案