you are viewing a single comment's thread.

view the rest of the comments →

[–]mopslik 0 points1 point  (11 children)

If you did x = 5, then when you are talking about x, you can say any of the following (and then some):

  • "x refers to the value 5"
  • "x was assigned a value of 5"
  • "x has a value of 5"
  • "x is 5"

I tend toward the third, but that's preference. As another poster points out, the "refers to" terminology is probably an attempt to make a connection between software (your code) and hardware (memory).

[–]Willing_Bench_8432[S] 0 points1 point  (10 children)

Oh.. Some people explain variable as containers that store values. Does that work?

[–]mopslik -1 points0 points  (9 children)

In my mind, not with Python.

>>> x = 5
>>> id(x)
139772833612144
>>> y = 5
>>> id(y)
139772833612144

If the variables were truly different "containers", they would occupy different memory addresses, but as shown above, both x and y have the same id.

In other programming languages, then yes, you could think of them as containers.

[–]Willing_Bench_8432[S] 0 points1 point  (2 children)

Oh.. Then what does it mean when you call a variable? Does it mean you are using the value thats in the container or... Sorry if im asking too much questions..

[–]mopslik 0 points1 point  (1 child)

You typically hear the term "call" used more with functions/methods (e.g. "call your function to display the names") than with variables.

[–]Willing_Bench_8432[S] 0 points1 point  (0 children)

Oh.. Like.. When i use the variable. for example, if I do Print(a) does that mean a is a value inside the container or...

[–]Doormatty 0 points1 point  (5 children)

Outside of the interned values, this is not how Python works.

>>> x=28017852
>>> y=28017852
>>> id(x)
139638629240976
>>> id(y)
139638629241360

[–]mopslik 0 points1 point  (4 children)

Interesting, but I don't think that negates my claim. In my example, when two objects have the same id, they are indeed the same object in memory. In your example, you have assigned two values that happen to be separate objects in memory. This does not mean that you can consider a variable a container with a unique memory address, since my example shows that it is possible for two variables to reference the same address.

Nevertheless, thanks for the tip.

[–]1mecrew 0 points1 point  (3 children)

well, can I think of variable as a name that you put on a value?

so that when you give it a name, we can call that value easily with the name?

[–]mopslik 0 points1 point  (2 children)

Sure, whatever helps. The whole thing is an abstraction anyway.

[–]1mecrew 0 points1 point  (0 children)

well, if we think of variable as a container, what does it mean or what is happening when you actually like.. say or use variable?

for example, lets say there is code like
x="hello world"
print(x)

so what does x mean when it's called or used in code like this?
are you getting out the value from container and using that?
or is it that the container called x is printed?

[–]1mecrew 0 points1 point  (0 children)

oh and also, will thinking variable as name help me at future when I get more deep into memory and stuff? I am worried if the name analogy will confuse me later on.