简体   繁体   English

`{...}`在python变量的打印输出中是什么意思?

[英]What does `{…}` mean in the print output of a python variable?

Someone posted this interesting formulation , and I tried it out in a Python 3 console: 有人发布了这个有趣的公式 ,我在Python 3控制台中试了一下:

>>> (a, b) = a[b] = {}, 5
>>> a
{5: ({...}, 5)}

While there is a lot to unpack here, what I don't understand (and the semantics of interesting character formulations seems particularly hard to search for) is what the {...} means in this context? 虽然这里有很多需要解压缩的东西,但是我不理解的东西(以及有趣的字符公式的语义似乎特别难以搜索)是{...}在这种情况下意味着什么? Changing the above a bit: 改变上面的一点:

>>> (a, b) = a[b] = {'x':1}, 5
>>> a
{5: ({...}, 5), 'x': 1}

It is this second output that really baffles me: I would have expected the {...} to have been altered, but my nearest guess is that the , 5 implies a tuple where the first element is somehow undefined? 这是第二个输出让我感到困惑:我本来期望{...}被改变了,但我最接近的猜测, 5意味着一个元组,其中第一个元素以某种方式未定义? And that is what the {...} means? 这就是{...}含义? If so, this is a new category of type for me in Python, and I'd like to have a name for it so I can learn more. 如果是这样,这是我在Python中的一个新类型,我希望有一个名称,以便我可以了解更多。

It's an indication that the dict recurses, ie contains itself. 这表明dict是recurses,即包含它自己。 A much simpler example: 一个更简单的例子:

>>> a = []
>>> a.append(a)
>>> a
[[...]]

This is a list whose only element is itself. 这是一个唯一元素本身的列表。 Obviously the repr can't be printed literally, or it would be infinitely long; 显然,repr不能按字面打印,否则会无限长; instead, the builtin types notice when this has happened and use ... to indicate self-containment. 相反,内置类型会在发生这种情况时发出通知并使用...来表示自我控制。

So it's not a special type of value, just the normal English use of "..." to mean "something was omitted here", plus braces to indicate the omitted part is a dict. 所以这不是一种特殊的价值,只是正常的英语使用“......”表示“这里省略了什么”,加上大括号表示省略的部分是一个字典。 You may also see it with brackets for a list, as shown above, or occasionally with parentheses for a tuple: 您也可以使用括号表示列表,如上所示,或偶尔使用括号表示元组:

>>> b = [],
>>> b[0].append(b)
>>> b
([(...)],)

Python 3 provides some tools so you can do this with your own objects, in the form of reprlib . Python 3提供了一些工具,因此您可以使用reprlib的形式对自己的对象执行此reprlib

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM