为什么我要提到“名字"?和“绑定"在 Python 中而不是“变量"和“分配"? [英] Why should I refer to "names" and "binding" in Python instead of "variables" and "assignment"?

查看:43
本文介绍了为什么我要提到“名字"?和“绑定"在 Python 中而不是“变量"和“分配"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我应该在 Python 中引用名称"和绑定"而不是变量"和赋值"?我知道这个问题有点笼统,但我真的很想知道:)

Why should I refer to "names" and "binding" in Python instead of "variables" and "assignment"? I know this question is a bit general but I really would like to know :)

推荐答案

在 C 和 C++ 中,变量是一个命名的内存位置.变量的值是存储在该位置的值.分配给变量并修改该值.所以变量是内存位置,而不是它的名字.

In C and C++, a variable is a named memory location. The value of the variable is the value stored in that location. Assign to the variable and you modify that value. So the variable is the memory location, not the name for it.

在 Python 中,变量是用于引用对象的名称.变量的值就是那个对象.到目前为止听起来是一样的.但是分配给变量并且您修改对象本身,而是更改变量引用的对象.所以变量是名字,而不是对象.

In Python, a variable is a name used to refer to an object. The value of the variable is that object. So far sounds like the same thing. But assign to the variable and you don't modify the object itself, rather you alter which object the variable refers to. So the variable is the name, not the object.

因此,如果您正在抽象地考虑 Python 的属性,或者如果您同时讨论多种语言,那么对这两种不同的事物使用不同的名称会很有用.为使事情简单明了,您可能会避免谈论 Python 中的变量,并将赋值运算符的作用称为绑定"而不是赋值".

For this reason, if you're considering the properties of Python in the abstract, or if you're talking about multiple languages at once, then it's useful to use different names for these two different things. To keep things straight you might avoid talking about variables in Python, and refer to what the assignment operator does as "binding" rather than "assignment".

请注意,Python 语法 将赋值"作为一种声明,而不是绑定".至少有一些 Python 文档调用名称变量.因此,仅在 Python 的上下文中,这样做也不正确.行话的不同定义适用于不同的上下文.

Note that The Python grammar talks about "assignments" as a kind of statement, not "bindings". At least some of the Python documentation calls names variables. So in the context of Python alone, it's not incorrect to do the same. Different definitions for jargon words apply in different contexts.

这篇关于为什么我要提到“名字"?和“绑定"在 Python 中而不是“变量"和“分配"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆