美文网首页
python值传递&引用传递

python值传递&引用传递

作者: NowBurn | 来源:发表于2019-10-30 20:53 被阅读0次
    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值传递还是引用传递

    相关文章

      网友评论

          本文标题:python值传递&引用传递

          本文链接:https://www.haomeiwen.com/subject/uppavctx.html