Variables Are Not Boxes; They Are More Like Labels



Start NorthStar

Python Learning Notes

Variables Are Not Boxes; They Are More Like Labels

A beginner-friendly Python note on understanding variables as names that point to objects.

Python 学习笔记

变量不是盒子,更像标签

一篇适合初学者的 Python 笔记,帮助学生把变量理解成指向对象的名字标签。

Share this article

Copy the link, open your phone share sheet, scan the QR code for WeChat, or send by email.




Email

Many Python beginners first hear that a variable is like a box, and the data is stored inside the box. This can feel helpful at the very beginning, but in Python, it can also create the wrong mental model.

A better way to think about variables is this: a variable name is more like a label. The object exists, and the name points to that object.

Common Mistake

Look at this line:

x = 123

Many students imagine a box named x, with the number 123 inside it.

The problem is that this makes x sound like a container. In Python, it is more accurate to say that 123 is a number object, and x is a name that refers to that object.

You can picture it like this:

x  ->  123

x is not the box. x is the label attached to the object 123.

Core Idea

In Python, variable names refer to objects. In beginner-friendly language:

a variable name points to a data object.

When you write:

x = 123

you can think of two steps:

  1. There is a number object, 123.
  2. The name x points to that object.

This is why the label model is better than the box model.

What Happens When We Reassign?

Now look at this:

x = 456

With the box model, you may imagine taking 123 out of the x box and putting 456 inside.

With the label model, the idea is clearer: a new number object, 456, appears, and the label x now points to 456.

Before: x  ->  123
After:  x  ->  456

The contents of a box did not get replaced. The name x now refers to a different object.

A Quick Code Check

We can use id() to observe object identity:

x = 123
print(id(x))

x = 456
print(id(x))

The exact output depends on your computer and Python runtime, but you will often see two different identities:

...different number...
...different number...

You do not need to memorize the details of id() right now. The important idea is that after reassignment, the name x refers to another object.

Comparison

Mental model Good for What can go wrong
Variable as a box Feels intuitive at first Makes it seem like the variable itself stores the object
Variable as a label Fits Python better Helps with references, reassignment, and mutable lists

Why This Matters

For x = 123, this may sound like a small difference. But when you learn lists, it becomes very important.

For example, later you will see code like this:

a = [1, 2, 3]
b = a

This does not necessarily create two separate list boxes. It can mean that two names, a and b, point to the same list object. Many beginner list bugs start from this misunderstanding.

The earlier you understand variables as names pointing to objects, the easier it will be to learn mutable objects, function parameters, and list behavior.

Practice

  1. Draw the label picture for score = 95. Which name points to which object?
  2. If you then write score = 100, which object does score point to now?
  3. Challenge: after a = [1, 2] and b = a, do you have two lists, or two names?

One-Sentence Takeaway

In Python, a variable is not a box that stores data; it is a name label that points to an object.

This idea will make lists, function parameters, and mutable objects much easier to understand later.

很多 Python 初学者第一次学变量时,会听到一个常见比喻:变量像盒子,数据放在盒子里。这个说法在刚入门时好像能帮人理解,但在 Python 里,它很容易带来误解。

更准确的想法是:变量名更像标签。数据对象先存在,变量名像一张标签一样贴到这个对象上。

常见误解

看到这一行代码:

x = 123

很多同学会想象成:有一个叫 x 的盒子,里面装着数字 123

这个比喻的问题是,它会让你以为 x 本身就是一个装东西的容器。可是在 Python 里,更接近事实的是:123 是一个数字对象,x 是指向这个对象的名字。

可以把它想成这样:

x  ->  123

x 不是盒子,x 是贴在 123 这个对象上的名字标签。

核心概念

Python 里的变量名引用对象。用更适合初学者的话说,就是:

变量名指向数据对象。

当你写:

x = 123

可以理解成两步:

  1. Python 有一个数字对象 123
  2. 名字 x 指向这个对象。

这就是为什么我们说变量更像标签,而不是盒子。

重新赋值时发生了什么?

再看下一行:

x = 456

如果你用“盒子”理解,可能会想:x 盒子里的 123 被拿出来,换成了 456

但用“标签”理解会更清楚:新的数字对象 456 出现后,标签 x123 移到了 456 上。

原来:x  ->  123
后来:x  ->  456

这不是盒子里的内容被倒出来再装进去,而是名字 x 改为指向另一个对象。

用代码观察一下

我们可以用 id() 简单观察对象身份:

x = 123
print(id(x))

x = 456
print(id(x))

输出会因电脑和运行环境不同而不同,但通常你会看到两个不同的对象身份:

...不同的数字...
...不同的数字...

这里不需要背 id() 的细节。你只要理解:重新赋值以后,名字 x 指向了另一个对象。

对比一下

比喻 适合情况 容易误导的地方
变量是盒子 刚接触赋值时好像直观 容易以为变量本身装着数据
变量是标签 更适合 Python 帮你理解引用、重新赋值和 list 可变性

为什么这个概念很重要?

现在看 x = 123,这个区别好像只是说法不同。但等你学到 list 的时候,它会非常重要。

比如后面你会看到这样的代码:

a = [1, 2, 3]
b = a

这不是复制了两个一模一样的 list 盒子,而是 ab 两个名字可能指向同一个 list 对象。很多 list 修改错误,都是从这里开始的。

所以,越早把变量理解成“名字指向对象”,后面越不容易被可变对象绕晕。

小练习

  1. 写出 score = 95 的标签图:谁指向谁?
  2. 如果接着写 score = 100,标签 score 现在指向哪个对象?
  3. 挑战题:思考 a = [1, 2]b = a 之后,ab 是两个 list,还是两个名字?

一句话记住

Python 里的变量不是装数据的盒子,而是指向对象的名字标签。

理解这个概念后,再学 list、函数参数和可变对象时会轻松很多。

Questions or feedback?

Have a question about this article or want to suggest an improvement? Send us a private message.

Send Feedback

有问题或建议?

如果你对本文有疑问,或希望提出改进建议,请给我们发送私密留言。

发送反馈

Back to Python Learning Notes
返回 Python 学习笔记

滚动至顶部