sets anomaly

Steven D'Aprano steve+comp.lang.python at pearwood.info
Thu Dec 8 00:27:41 EST 2016


On Thursday 08 December 2016 02:17, Rustom Mody wrote:

> Trying to write some code using sets (well frozen sets)
> And was hit by this anomaly
> 
> This is the behavior of lists I analogously expect in sets:
> 
>>>> []
> []
>>>> [[]]
> [[]]
>>>> 
> 
> ie the empty list and the list of the empty list are different things

That's a property of the *list display*, [], not the list() constructor. The 
list constructor takes an iterable of items, so if you pass it an empty 
iterable, you get an empty list:

py> list([])
[]

Since there's no frozenset display, there's no analogy with [[]], and you 
similarly get a single empty frozen set:

py> frozenset([])
frozenset()

Notice the repr()? Like list(), tuple(), set() and dict(), calling frozenset() 
with no arguments returns an empty frozenset:

py> frozenset()
frozenset()


> And then some figuring out how to get an empty set into a set
> This is the best I get:
>>>> f([f([])])
> frozenset({frozenset()})


py> Ø = frozenset()
py> frozenset([Ø])
frozenset({frozenset()})



-- 
Steven
"Ever since I learned about confirmation bias, I've been seeing 
it everywhere." - Jon Ronson



More information about the Python-list mailing list