Is a variable the name, the value, or the memory location?

前端 未结 4 748
遥遥无期
遥遥无期 2020-11-30 11:39

I\'ve been learning Python for a few months, and also know very little C, and I was wondering if anyone could clear this doubt up for me:

Is a variable the name, the

相关标签:
4条回答
  • 2020-11-30 12:10

    There is a space somewhere in a memory where the values are stored. These memory is then located through address which is usually represented by hexadecimal number, or sometimes binary number. So, these numbers are hard to remember for programmer, so they used the term variable to refer to that memory location in a easy way, or you can say higher abstraction. So, x is just a name that refers to some location in the memory which contains value of 5. That's all!

    0 讨论(0)
  • 2020-11-30 12:21

    A variable is a mapping of a name to associated storage and serves as a way to refer to that storage location in your program. When you assign to a variable and perform operations on it, you're populating and altering that storage. When you reassign a variable, you can point it to a different storage location -- you are remapping that name.

    0 讨论(0)
  • 2020-11-30 12:31

    There are several things going on with the statement x=5:

    1. an int object with the value of 5 is created (or found if it already exists);
    2. the name x is created (or disassociated with the last object 'x' labeled);
    3. the reference count to the new (or found) int object is increased by 1;
    4. the name x is associated with the object with the value '5' created (or found).

    Since int objects are immutable, they may be interned for efficiency. String objects are more likely to be interned.

    Here are some examples:

    >>> x=5    # discussed
    >>> id(x)  # the 'id' which in cPython is the memory address.
    140246146681256
    >>> y=x    # now two names, 'x' and 'y' associated with that object
    >>> id(y)  
    140246146681256     # same object
    >>> z=5    # no guaranteed, likely the same object referred to by 'x' and 'y'
    >>> id(z)
    140246146681256     # id is the same! The object labeled 'x' was found and labeled 'z'
    >>> del x           # ref count to object '140246146681256' decreased by 1
    >>> del y           # same
    >>> z
    5
    >>> id(z)
    140246146681256    # same object but the names ''x' and 'y' no longer label it
    

    The best way to think of variables names, like 'x' in x=5 is as a label.

    It is possible to have objects with no label, such as the 5 individual string objects created in this list comprehension:

    >>> li=[str(x) for x in range(5)]  
    >>> li
    ['0', '1', '2', '3', '4']
    

    You can then create objects that match the value of those same 5 objects independently:

    >>> li2=list('012345')    # longer list; completely different construction
    >>> li2
    ['0', '1', '2', '3', '4', '5']    
    

    You can get their individual memory addresses (in cPython) or a unique id address:

    >>> [id(x) for x in li]
    [4373138488, 4372558792, 4372696960, 4373139288, 4373139368]
    >>> [id(x) for x in li2]
    [4373138488, 4372558792, 4372696960, 4373139288, 4373139368,  4372696720]  
    

    Note that the two independently created lists of anonymous object are the same (for the first 5 in this case). I purposely used strings because they are more likely to be interred...

    So think of it this way: two different processes are happening with x=5:

    1. the object is created (or found if it is immutable, interned and exists) and
    2. the object is labeled.
    0 讨论(0)
  • 2020-11-30 12:35

    In Python variables are best considered as names.

    In the case of numbers, numbers are immutable so reassigning a variable will always make the variable point to a different number without changing the number.

    x = 1
    y = x
    x += 1 # y is still 1
    

    The difference is clearer with mutable objects, where you can change a value OR make the variable refer to a different value

    x = [1,2,3]
    y = x
    x.append(4) # I mutated x's value, so y is now [1,2,3,4]
    x = [] # I made x point to a different list, so y is still [1,2,3,4]
    

    Recommended reading:

    http://web.archive.org/web/20180411011411/http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html

    http://me.veekun.com/blog/2012/05/23/python-faq-passing/

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