为什么要在6个内置常量中分配2个? [英] Why are 2 of the 6 built-in constants assignable?

查看:60
本文介绍了为什么要在6个内置常量中分配2个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

内置常量(不包括 site 常量),则表示:

In the documentation on built-in constants (excluding site constants) it's stated that:


注意:名称错误真实 __调试__ 无法重新分配(分配给它们,即使作为属性名称,也会引发 SyntaxError ),因此可以将它们视为真常量。

Note: The names None, False, True and __debug__ cannot be reassigned (assignments to them, even as an attribute name, raise SyntaxError), so they can be considered "true" constants.

如果我没记错的话, True False 在Python 3中变成了真正的竞争者。(如副本中所述。)

If I'm not mistaken, True and False became "true" contants in Python 3. (As also described in the duplicate.)

问题是,为什么其他两个不是(省略号未实现)真竞争者?是否有重新使用这些限制的用例(也许是 numpy Ellipsis ?)使它们不受此限制?

Question is, why aren't the other two (Ellipsis, NotImplemented) "true" contants? Is there a use case for re-assigning these (numpy with Ellipsis perhaps?) that has exempted them from this limitation?

标准库类型省略号未实现类型从行为上讲,它与 None 类型相同。即:

Exacerbating my confusion, in the documentation for standard library types, both the Ellipsis and NotImplemented types are, behaviorally, identical to the None type. Namely:


  • 它们是单例

  • 它们不支持特殊操作。

此问题与建议的重复项无关:为什么在Python 3中将True和False更改为关键字。它提出了完全不同的问题,即为什么 Ellipsis NotImplemented 不是 true常量 not 为什么将 True False 更改为1。

This question is not related to the proposed duplicate: Why were True and False changed to keywords in Python 3. It asks something completely different, namely, why Ellipsis and NotImplemented are not "true" constants and not why True and False where changed into ones.

推荐答案

您可以分配给任何非关键字的有效标识符。 True False None 的特殊之处在于,它们都是都是关键字和标识符。您可以在以下问题中了解其原因:

You can assign to any valid identifier that is not a keyword. What is special about True, False, None is that they are both keywords and identifiers. You can read about the reasoning for that in this question:

为什么在Python 3中将True和False更改为关键字

诸如未实现省略号不是特殊情况, int ,列表类型等。分配给 NotImplemented 不会更改内置常量。而是将名称 Ellipsis 绑定到当前作用域中的其他值。原始的省略号不变。

Builtins such as NotImplemented or Ellipsis are not special cases, and neither are int, list, type and so on. Assigning to NotImplemented doesn't change the builtin constant. Instead you bind the name Ellipsis to a different value in the current scope. The original Ellipsis is not changed.

分配关键字是SyntaxError。

Assigning to a keyword is a SyntaxError.

您可以通过导入关键字模块来查看关键字列表。

You can see the list of keywords by importing the keywords module.

>>> import keyword
>>> keyword.kwlist

['False',
 'None',
 'True',
 'and',
 'as',
 'assert',
 'break',
 'class',
 'continue',
 'def',
 'del',
 'elif',
 'else',
 'except',
 'finally',
 'for',
 'from',
 'global',
 'if',
 'import',
 'in',
 'is',
 'lambda',
 'nonlocal',
 'not',
 'or',
 'pass',
 'raise',
 'return',
 'try',
 'while',
 'with',
 'yield']

很多内置标识符不在此列表中,您可以将新值分配给 int 省略号

There are lots of builtin identifiers that are not in this list, and you can assign new values to int, Ellipsis etc.

... 是特例,因为它不是有效的标识符名称,因此将无法分配。

... is a special case, since it's not a valid identifier name in the first place, so it would be impossible to assign to.

与许多其他语言相比,Python使保留关键字的列表很短。原因之一可能是保持与代码的向后兼容性,出于某种原因,该代码在使用 Ellipsis 等标识符成为语言的一部分之前就使用了它。

Python keeps the list of reserved keywords quite short, compared to many other languages. One reason is probably to keep backwards compatibility with code that for some reason used identifiers such as Ellipsis before it became part of the language.

这篇关于为什么要在6个内置常量中分配2个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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