Trucy Luce
Trucy Luce

Reputation: 21

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?

Upvotes: 0

Views: 49

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1123590

Because the {...} syntax wasn't introduced until Python 2.7, and by that time the set([...]) repr() format was already established.

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. Python 3 had {...} notation for sets from the start.

Upvotes: 4

Related Questions