代码之家  ›  专栏  ›  技术社区  ›  Alec

关于内存地址的变量分配

  •  2
  • Alec  · 技术社区  · 5 年前

    a = 1
    b = 2
    

    现在,我来分配 a b

    a = b
    

    如所料, a == 2 已设置为的内存地址 .

    但事实上,没有。如果我有

    b += 1
    

    2 . 为什么 不指向

    2 回复  |  直到 5 年前
        1
  •  3
  •   imposeren Dennis    5 年前

    示例中的行为如下

    In [1]: a = 1                                                                                                                                                                     
    
    In [2]: b = 2                                                                                                                                                                     
    
    In [3]: a = b                                                                                                                                                                     
    
    In [4]: b+=1                                                                                                                                                                      
    
    In [5]: b                                                                                                                                                                         
    Out[5]: 3
    
    In [6]: a                                                                                                                                                                         
    Out[6]: 2
    

    在这个例子中,当你 a=b b += 1 ,将1添加到b的操作,创建一个新的整数值 3 因为b和b指向那个值,但a仍然指向旧值 2

    In [1]: a = [1]                                                                                                                                                                   
    
    In [2]: b = [2]                                                                                                                                                                   
    
    In [3]: a = b                                                                                                                                                                     
    
    In [4]: b.append(2)                                                                                                                                                               
    
    In [5]: a                                                                                                                                                                         
    Out[5]: [2, 2]
    
    In [6]: b                                                                                                                                                                         
    Out[6]: [2, 2]
    
    In [7]: b += [3, 4];                                                                                                                                                                        
    
    In [8]: b
    Out[8]: [2, 2, 3, 4]
    
    In [9]: a
    Out[9]: [2, 2, 3, 4]
    
    

    现在这里发生了什么?我们改变了 b 但是 a append 或者更新列表 in-place 从那以后 结果都更新了!

    += 运算符由定义 __iadd__ int -所有的 __iXXX__ 方法返回int的新实例 list -s公司 __iadd__(self, other) self.extend(other); return self

    因此,甚至整数也可以作为一个列表,例如 here

        2
  •  0
  •   hem rajat1293    5 年前

    python中的变量是引用。很少有对不可变对象(如字符串、整数等)的引用,而列表和集合是可变的。

    Python 3.7.2 (tags/v3.7.2:9a3ffc0492, Dec 23 2018, 22:20:52) [MSC v.1916 32 bit (Intel)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    # Consider two variables x & y  with integers 23 and 46. They have immutable references
    # Meaning, if y=x, it doesn't mean y will change whenever x is updated.
    >>> x=23
    >>> print (x)
    23
    >>> y=46
    >>> print (x)
    23
    >>> print (y)
    46
    >>> y=x
    >>> print (x)
    23
    >>> print (y)
    23
    # Let's change the value of x after the assignment
    >>> x=99
    >>> print (x)
    99
    # Since it is immutable, the value wont change.
    >>> print (y)
    23
    #Let's consider the mutable reference scenario. a & b are two lists which have mutable references
    # Meaning, if b=a, it means b will change whenever a is changed
    >>> a = list([11,22,33,87])
    >>> print (a)
    [11, 22, 33, 87]
    >>> b = list([87,53,98,2])
    >>> print (b)
    [87, 53, 98, 2]
    >>> a=b
    >>> print (a)
    [87, 53, 98, 2]
    >>> print (b)
    [87, 53, 98, 2]
    # Popping the list would alter b
    >>> b.pop()
    2
    >>> print (b)
    [87, 53, 98]
    # Notice the change in value of a
    >>> print (a)
    [87, 53, 98]
    >>>
    
        3
  •  0
  •   Toothpick Anemone    5 年前


    他们有这个名字是因为他们“指向”事物。
    a b , 指向 2 ,等等。

    你所期待的事情会发生:

    enter image description here

    实际发生了什么:

    picture of actual behavior