使用 True/False 作为键 - 如何/为什么这样做? [英] Using True/False as keys - how/why does this work?
问题描述
我很乐意使用这种简单的语法来初始化 dictionary
d = {'a':'Apple','b':'Bat'};
今天在看一个页面的时候,遇到了这段奇怪的代码
{True:0, False:1}[True];
我想知道为什么/如何工作?True
和 False
是保留关键字,所以,这种疯狂的语法应该毫无意义(对于编译器),但它是不是.
这变得更疯狂了
<预><代码>>>>d = dict(真 = 0, 假 = 1);语法错误:分配给关键字>>>d = dict(_True = 0, _False = 1);>>>d{'_假':1,'_真':0}在dict()
构造函数中,不允许使用True
关键字!但是……
<小时>更新
Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 32 bit (Intel)] on win32输入copyright"、credits"或license()"以获取更多信息.>>>导入关键字>>>关键字.iskeyword('打印');错误的>>>关键字.iskeyword('else');真的>>>关键字.iskeyword('真');真的>>>关键字.iskeyword('假');真的
虽然它们是关键字(在 Python 3 中),True
和 False
仍然是对象的名称(它们分别是 bool(1)
和 bool(0)
).
所以你可以在任何有意义的地方使用它们.由于它们是可散列的,这包括将它们用作字典键.你可以这样做:
d = {}d[真] = "真"d[假] = "假"
你可以用大括号直接得到同一个字典(d = {True: "True", False: "False"}
),但不能使用dict
带有关键字参数的构造函数.构造函数的关键字版本仅构建具有字符串键的字典,并且这些字符串必须是合法的 Python 标识符.True
和 False
不是字符串,也不能(因为它们是关键字)用作标识符.
您当然可以使用键/值元组构造函数的序列,如果有某种原因您需要调用 dict
而不是使用括号:
d = dict([(True, "True"), (False, "False")])
我注意到您展示的代码片段有点傻.Python 中的 bool
类型是 int
的子类,所以你可以对 True
或 False
进行数学运算,如果你要.字典索引代码等价于1-True
,即0
.
I am comfortable using this simple syntax for initializing a dictionary
d = {'a':'Apple','b':'Bat'};
Today, while reading a page, I encountered this weird piece of code
{True:0, False:1}[True];
I was wondering why/how that could work? True
and False
are reserved keywords,
and so, that crazy syntax should be meaningless (for the compiler), but it is
not.
>>> d = {True:0, False:1};
>>> d
{False: 1, True: 0}
And this gets crazier
>>> d = dict(True = 0, False = 1);
SyntaxError: assignment to keyword
>>> d = dict(_True = 0, _False = 1);
>>> d
{'_False': 1, '_True': 0}
In dict()
constructor, True
keyword is not allowed! But...
Update
Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> import keyword
>>> keyword.iskeyword('print');
False
>>> keyword.iskeyword('else');
True
>>> keyword.iskeyword('True');
True
>>> keyword.iskeyword('False');
True
While they are keywords (in Python 3), True
and False
are still names of objects (they are, respectively bool(1)
and bool(0)
).
So you can use them anywhere a value makes sense. Since they're hashable, that includes using them as dictionary keys. You can do:
d = {}
d[True] = "True"
d[False] = "False"
You can get the same dictionary directly with curly brackets (d = {True: "True", False: "False"}
), but not using the dict
constructor with keyword arguments. The keyword version of the constructor only builds dicts with keys that are strings, and those strings must be legal Python identifiers. True
and False
are not strings, nor (since they're keywords) can they be used as identifiers.
You can of course use the sequence of key/value tuples constructor, if there's some reason you need to be calling dict
rather than using brackets:
d = dict([(True, "True"), (False, "False")])
I would note that the code fragment you show is a bit silly. The bool
type in Python is a subclass of int
, so you can do mathematical operations on True
or False
if you want. The dictionary indexing code is equivalent to 1-True
, which will be 0
.
这篇关于使用 True/False 作为键 - 如何/为什么这样做?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!