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

I have been learning Python for several months, and also know very little C, and I was wondering if anyone would be able to resolve this doubt for me:

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

For example:

x = 5 

Is the variable x, value x, or location x in memory?

I am looking for a clear explanation of what a variable is. I already reviewed the Wikipedia page on variables and this question , but neither were too clear for me. If this is a duplicate question, a link to the correct answer will be great.

Thanks!

+8
variables python variable-assignment
source share
4 answers

There are several things in the x=5 instruction:

  • an int object with a value of 5 (or found if it already exists);
  • the name x is created (or disassociated with the last marked "x");
  • the reference counter for a new (or found) int object is incremented by 1;
  • name x is associated with an object with a value of '5' created (or found).

Because int objects are immutable, they can be interned to increase 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 about variable names, such as 'x' at x=5 , is to label.

It is possible that there are objects without a label, such as 5 separate string objects created in this understanding of the list:

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

Then you can create objects that match the value of the same 5 objects independently:

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

You can get your 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 the anonymous object are the same (for the first 5 in this case). I intentionally used strings because they are more likely to be buried ...

Think of it this way: two processes happen with x=5 :

  • an object is created (or found if it is immutable, interned and exists) and
  • object is marked.
+8
source share

There is a place in memory where values ​​are stored. This memory is then located through an address, which is usually represented by a hexadecimal number or sometimes a binary number. Thus, these numbers are hard to remember for the programmer, so they used a type variable to easily access this memory cell, or you can say a higher abstraction. So x is just a name that refers to some place in memory that holds the value 5. That's all!

+3
source share

A variable is a mapping of a name to an associated repository and serves as a way of referencing this storage location in your program. When you assign a variable and perform operations on it, you populate and modify this repository. When you reassign a variable, you can point it to another storage location - you reassign this name.

0
source share

In Python, variables are best treated as names.

In the case of numbers, numbers are immutable, so redefining a variable will always point the variable to another 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 the value OR make the variable a reference to another value

 x = [1,2,3] y = x x.append(4) # I mutated x 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://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html#other-languages-have-variables

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

0
source share

All Articles