Python 中的变量名不能以数字开头,或者可以吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41962391/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 01:55:50  来源:igfitidea点击:

Variable names in Python cannot start with a number or can they?

pythonvariable-names

提问by steffen

This is somewhat academic, but nevertheless.

这有点学术性,但仍然如此。

Python syntax forbids starting a variable name with a number, but this can be sidestepped like so:

Python 语法禁止以数字开头变量名,但这可以像这样回避:

>>> globals()['1a'] = 1
>>> globals()['1a']
1

Likewise for locals().

同样对于locals().

Does that mean that Python actually allows it, and that it's just not very visible?

这是否意味着 Python 实际上允许它,并且它不是很明显?

edit:

编辑:

My question is not whether it is allowed; I am aware that it is formally not allowed in Python. The question is why can I work around it by addressing globals()directly, and if that breaks certain rules or guidelines, or if it maybe even have a good reason/application to allow that.

我的问题不是是否允许;我知道它在 Python 中是正式不允许的。问题是为什么我可以通过globals()直接解决来解决它,如果这违反了某些规则或指导方针,或者它甚至可能有一个很好的理由/应用程序来允许这样做。

采纳答案by Uriel

Python parser forbids naming variables that way, for the sake of parsing numbers and variables separately, as naming a variable 1e1would create a chaos - is it the number 10.0or the variable 1e1?

Python 解析器禁止以这种方式命名变量,为了分别解析数字和变量,因为命名变量1e1会造成混乱 - 是数字10.0还是变量1e1

"Python, please output for me 1e1!" - "Why is it 10.0? I stored 100 over there!"

“Python,请给我输出1e1!” - “为什么是10.0?我在那边存了100!”

Butthe variables are actually stored in a way that allows binding a string that starts with a number to a value, because that feature is no harm in hashing maps of any kind, and so using this "trick" you can achieve your wanted numeral-prefixed-name variable without hurting the parser severability.

但是变量实际上是以允许将一个以数字开头的字符串绑定到一个值的方式存储的,因为该功能对任何类型的哈希映射都没有害处,因此使用这个“技巧”可以实现您想要的数字-前缀名称变量而不会损害解析器的可分割性。

I would say that technically, naming variables in that manner is not a violationto python guidelines, but it is highly discouraged, and as a rule unnecessary. Using globalsfor injecting variables is known as a very bad practiceand this case should not be an outstanding.

我会说,从技术上讲,以这种方式命名变量并不违反Python 准则,但强烈不鼓励这样做,而且通常是不必要的。使用globals注射变量被称为一个非常不好的做法和这种情况下,不应该是一个优秀的。



Of course, python could have used an encloser to numerals like strings, say *123*, but I believe the intent of inventing python was to make programming easier, not stretching the limits of variable naming space.

当然,python 可以使用像字符串这样的数字的封闭器*123*,但我相信发明 python 的目的是使编程更容易,而不是扩展变量命名空间的限制。



Practically speaking, if you mustuse number-headed names you better do it with your own dictionary, rather than globals:

实际上,如果您必须使用以数字开头的名称,您最好使用自己的字典来做,而不是globals

>>> number_headed_vars = {'1a': 100}
>>> number_headed_vars['1a']
100

That way you can create your own variables system - and avoid abusing globals().

这样您就可以创建自己的变量系统 - 并避免滥用globals().

回答by Ofer Arial

This is what you can and can't do with that 1ain globals. You can't really use it in a variable, unless you use all of it's definition in globals (I mean accessing that dictionary), which makes it very uncomfortable for usage (another reason for not doing that).

这是您1a在全局变量中可以做和不能做的事情。你不能真正在变量中使用它,除非你在全局变量中使用它的所有定义(我的意思是访问该字典),这使得使用起来非常不舒服(不这样做的另一个原因)。

Basically, 1ais not a real variable as a1, as shown in the following output:

基本上,1a不是真正的变量 as a1,如以下输出所示:

>>> globals()['1a'] = 1
>>> globals()['1a']
1

>>> a = 1a
File "<stdin>", line 1
    a = 1a
         ^
SyntaxError: invalid syntax

>>> a = globals()['1a']
>>> a
1

>>> globals()['a1'] = 5
>>> a = a1
>>> a
5