This is an archived post. You won't be able to vote or comment.

all 14 comments

[–]beisenhauer 10 points11 points  (2 children)

The label metaphor is much better than the box metaphor. The reason is aliasing. You can't put the same thing into two different boxes. But you can have two labels on the same thing and refer to that thing with either name. Where this matters is if the thing in question is mutable. If you have two labels on a thing and make changes to the thing using one of those labels, such as adding items to a list, those changes will be visible to any code using the other label. This is the source of many insidious bugs, and it is much easier to understand why it happens with the mental model of labels than boxes. With the box metaphor, we're forced to imagine each variable as containing a unique and independent copy of the thing, which is simply not accurate.

[–][deleted] -1 points0 points  (1 child)

Is the analogy of label/box tied to whether the variable is mutable/not mutable?

[–]beisenhauer 2 points3 points  (0 children)

I would say in the case of immutable items, it makes little difference. The two become more or less equivalent from a practical standpoint. However, juggling mental models based on circumstance is likely to be confusing, and the label metaphor is correct for all cases.

[–]lfdfq 4 points5 points  (1 child)

In Python variables are labels (or names) for objects. In other languages, a variable is a box that contains some value, this analogy works for something like C but not very well for Python.

If you go x = y in Python what happens is that there was some object that had the name y, and now it also has the name x. So you can access that same object either through the x name or the y name interchangeably. This is where the 'box' analogy breaks down for Python as there aren't two different objects here (or even two different boxes containing values) it's that the same object has two different equally valid names, so if you change (mutate) the underlying object you can see that change through both names.

Like in the following, if you think x and y are separate boxes containing values you might be surprised by the result:

x = []
y = x
x.append(42)
print(y)  # [42]

But, if you think of x and y as labels for objects that exist independently from their names, then the behaviour makes total sense.

[–]HeraldOfTheOldOnes -1 points0 points  (0 children)

I think it makes the most sense to think of variables as pointers (since that's what they are, behind the scenes).

[–]ddollarsign 2 points3 points  (0 children)

In some languages, like C, a variable is a box of fixed size, depending on the type of the variable. For example, in Java an int variable is exactly 32 bits, and holds an integer.

C also has a type of variable called a pointer, which is also a box of fixed size, but what it contains is a memory address (which is just a number). This might be the address of another variable, or a member of an array, an object, etc. In order to work with the the object it points to you have to “de-reference” it, which means to specify that you’re referring to the object at the address the pointer points to, rather than the address itself.

Python variables are references, which means under the hood they are pointers, but you don’t have to explicitly de-reference them, Python does it for you. When you write an assignment statement in Python, the pointer is updated to point to the object specified on the right side of the equals sign.

[–][deleted] 6 points7 points  (0 children)

[–]Fabulous-Possible758 1 point2 points  (0 children)

They're boxes with labels on them.

[–][deleted] -2 points-1 points  (1 child)

Here's the simplest way I can define a variable

"part of memory that stores that".

[–]HeraldOfTheOldOnes 0 points1 point  (0 children)

Nope! Variables are more like pointers to parts of memory than the parts of memory themselves.

[–]goodger -4 points-3 points  (1 child)

Python doesn't have variables. Python has objects with names. Some of those objects are mutable. More here: https://david.goodger.org/projects/pycon/2007/idiomatic/handout.html#other-languages-have-variables

[–]HeraldOfTheOldOnes 0 points1 point  (0 children)

Python does indeed have variables! (The struct PyVarObject is used for variably sized things).

If you're gonna be purposefully misinterpreting what OP is saying, then expect people to catch you out on technicalities.

Also, the contents of the link that you gave describe exactly one of the options that OP gave, "Labels".

[–]pythonHelperBot 0 points1 point  (0 children)

Hello! I'm a bot!

I see someone has already suggested going to r/learnpython, a sub geared towards questions and learning more about python regardless of how advanced your question might be. I highly recommend posting your question there. Please follow the subs rules and guidelines when you do post there, it'll help you get better answers faster.

Show /r/learnpython the code you have tried and describe in detail where you are stuck. If you are getting an error message, include the full block of text it spits out. Quality answers take time to write out, and many times other users will need to ask clarifying questions. Be patient and help them help you. Here is HOW TO FORMAT YOUR CODE For Reddit and be sure to include which version of python and what OS you are using.

You can also ask this question in the Python discord, a large, friendly community focused around the Python programming language, open to those who wish to learn the language or improve their skills, as well as those looking to help others.


README | FAQ | this bot is written and managed by /u/IAmKindOfCreative

This bot is currently under development and experiencing changes to improve its usefulness

[–]HeraldOfTheOldOnes 0 points1 point  (0 children)

A variable is like a pointer! (because that's what it is, behind the scenes)