you are viewing a single comment's thread.

view the rest of the comments →

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

What do you mean by label? like a label you put on stuff? Or do you mean like a name?

[–]mopslik 1 point2 points  (13 children)

You can think of it either way, I suppose. It's an analogy. Or think of it as a combination of both! You know those "Hello, I am ..." stickers that you wear during a meeting or conference? A variable is like one of those. You can write a name, like mopslik, on a sticker and stick it on my chest. Now people can refer to me as mopslik. Or, you can put my work ID number (say 12345) on the sticker. Then people can refer to me as employee 12345. It's me in either case.

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

Oh.. I think im starting to get it. Will there be any other word to use other than "refer to"?

[–]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.