美文网首页
Python pass by assignment

Python pass by assignment

作者: __小赤佬__ | 来源:发表于2019-04-28 02:34 被阅读0次

    在C++里pass by value和pass by reference是两个很容易区分的东西。但是在python中,让我有点糊涂,其实Python是“pass by assignment”。


    举个例子:

    >>> a=[1,2,3]
    >>> b=a
    >>> b
    [1, 2, 3]
    >>> a.append(4)
    >>> b
    [1, 2, 3, 4]
    >>> a=[1]
    >>> b
    [1, 2, 3, 4]
    

    可以看到,a和b一开始都是bind to [1,2,3]这一个list object。这个list object的变化(append 4)使得a和b都发生了变化。但是,当a bind to another object [1], b is still pointing to the original object, and the original object doesn't change.

    This property properly determines when calling a function, how the parameter is passed. Personally I would understand that as "pass by object".

    Reference

    stack overflow and quora

    相关文章

      网友评论

          本文标题:Python pass by assignment

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