Are python variables pointers? or else what are they?

前端 未结 9 1907
借酒劲吻你
借酒劲吻你 2020-11-22 07:01

Variables in Python are just pointers, as far as I know.

Based on this rule, I can assume that the result for this code snippet:

i = 5
j = i
j = 3          


        
相关标签:
9条回答
  • 2020-11-22 07:32

    They are not quite pointers, they are references to objects. Objects can be either mutable, or immutable. An immutable object is copied when it is modified. A mutable object is altered in-place. An integer is an immutable object, that you reference by your i and j variables. A list is a mutable object.

    In your first example

    i=5
    # The label i now references 5
    j=i
    # The label j now references what i references
    j=3
    # The label j now references 3
    print i
    # i still references 5
    

    In your second example:

    i=[1,2,3]
    # i references a list object (a mutable object)
    j=i
    # j now references the same object as i (they reference the same mutable object)
    i[0]=5
    # sets first element of references object to 5
    print j
    # prints the list object that j references. It's the same one as i.
    
    0 讨论(0)
  • 2020-11-22 07:33

    When you set j=3 the label j no longer applies (points) to i, it starts to point to the integer 3. The name i is still referring to the value you set originally, 5.

    0 讨论(0)
  • 2020-11-22 07:34

    what ever variable is on the left side of '=' sign is assigned with the value on the right side of '='

    i = 5

    j = i --- j has 5

    j = 3 --- j has 3 (overwrites the value of 5) but nothing has been changed regarding i

    print(i)-- so this prints 5

    0 讨论(0)
  • 2020-11-22 07:42

    We call them references. They work like this

    i = 5     # create int(5) instance, bind it to i
    j = i     # bind j to the same int as i
    j = 3     # create int(3) instance, bind it to j
    print i   # i still bound to the int(5), j bound to the int(3)
    

    Small ints are interned, but that isn't important to this explanation

    i = [1,2,3]   # create the list instance, and bind it to i
    j = i         # bind j to the same list as i
    i[0] = 5      # change the first item of i
    print j       # j is still bound to the same list as i
    
    0 讨论(0)
  • 2020-11-22 07:44

    In Python, everything is object including the memory pieces themselves that you are returned. That means, when new memory chunk is created (irrespective of what've you created: int, str, custom object etc.), you have a new memory object. In your case this is the assignment to 3 which creates a new (memory) object and thus has a new address.

    If you run the following, you see what I mean easily.

    i = 5
    j = i
    print("id of j: {}", id(j))
    j = 3
    print("id of j: {}", id(j))
    

    IMO, memory wise, this is the key understanding/difference between C and Python. In C/C++, you're returned a memory pointer (if you use pointer syntax of course) instead of a memory object which gives you more flexibility in terms of changing the referred address.

    0 讨论(0)
  • 2020-11-22 07:47

    Assignment doesn't modify objects; all it does is change where the variable points. Changing where one variable points won't change where another one points.

    You are probably thinking of the fact that lists and dictionaries are mutable types. There are operators to modify the actual objects in-place, and if you use one of those, you will see the change in all variables pointing to the same object:

    x = []
    y = x
    x.append(1)
    # x and y both are now [1]
    

    But assignment still just moves the pointer around:

    x = [2]
    # x now points to new list [2]; y still points to old list [1]
    

    Numbers, unlike dictionaries and lists, are immutable. If you do x = 3; x += 2, you aren't transforming the number 3 into the number 5; you're just making the variable x point to 5 instead. The 3 is still out there unchanged, and any variables pointing to it will still see 3 as their value.

    (In the actual implementation, numbers are probably not reference types at all; it's more likely that the variables actually contain a representation of the value directly rather than pointing to it. But that implementation detail doesn't change the semantics where immutable types are concerned.)

    0 讨论(0)
提交回复
热议问题