python byRef // copy

前端 未结 4 1575
花落未央
花落未央 2021-01-05 16:52

I am new to Python (and dont know much about programming anyway), but I remember reading that python generally does not copy values so any statement a = b makes b point to a

4条回答
  •  离开以前
    2021-01-05 17:27

    You can see what you were expecting with just one small change. The two variables do indeed start out pointing to the same object, and if that object is mutable you can see a change in both places at once.

    >>> a = [1]
    >>> b = a
    >>> a[0] = 2
    >>> print b
    [2]
    

    What you did with your example was to change a so that it no longer referred to the object 1 but rather to the object 2. That left b still referring to the 1.

提交回复
热议问题