简体   繁体   English

在__str__和直接打印之间输入'set'的区别

[英]type 'set''s difference between __str__ and printing directly

In [1]: import sys

In [2]: sys.version_info
Out[2]: sys.version_info(major=3, minor=5, micro=2, releaselevel='final', serial=0)

In [3]: b=set([10,20,40,32,67,40,20,89,300,400,15])

In [4]: b
Out[4]: {10, 11, 15, 20, 32, 40, 67, 89, 111, 300, 400}
In [1]: import sys

In [2]: sys.version_info
Out[2]: sys.version_info(major=2, minor=7, micro=12, releaselevel='final', serial=0)

In [3]: b=set([10,20,40,32,67,40,20,89,300,400,15])

In [4]: b
Out[4]: set([32, 67, 40, 10, 11, 300, 15, 400, 20, 89, 111])

why have this different between 2 and 3? 为什么2和3之间有区别?

Because the {...} syntax wasn't introduced until Python 2.7 , and by that time the set([...]) repr() format was already established. 因为{...}语法直到Python 2.7才被引入 ,并且那时set([...]) repr()格式已经建立。

So to keep existing Python 2 code that may have relied on the set([...]) representation working, the repr() wasn't changed in the 2.x series. 因此,为了保持可能依赖于set([...])表示形式的现有Python 2代码,在2.x系列中未更改repr() Python 3 had {...} notation for sets from the start. Python 3从一开始就使用{...}表示集合。

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

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