python值传递&引用传递

2019-10-30  本文已影响0人  NowBurn
def func(val1):
    print('val1: {}, id: {}'.format(val1, id(val1)))    # val1: [1, 2, 3], id: 43499976
    val2 = val1
    print('val2: {}, id: {}'.format(val2, id(val2)))    # val2: [1, 2, 3], id: 43499976
    val2.append(4)
    print('val2: {}, id: {}'.format(val2, id(val2)))    # val2: [1, 2, 3, 4], id: 43499976
    val2 = val2 + [5]
    print('val2: {}, id: {}'.format(val2, id(val2)))    # val2: [1, 2, 3, 4, 5], id: 43500296
 
a = [1, 2, 3]
print('a: {}, id: {}'.format(a, id(a)))     # a: [1, 2, 3], id: 43499976
func(a)
print('a: {}, id: {}'.format(a, id(a)))     # a: [1, 2, 3, 4], id: 43499976

参考

  1. Python值传递还是引用传递
上一篇下一篇

猜你喜欢

热点阅读