Set literal confusion

kaoruAngel kaoruangel at gmail.com
Tue Sep 22 12:55:09 EDT 2009


I recently decided to implement a small project in python after being
away from the language for a while, so, in learning the language over
again, I experimented.

---------------------------------------
Python 3.1.1 (r311:74483, Aug 17 2009, 16:45:59) [MSC v.1500 64 bit
(AMD64)] on win32

>>> 5 in set(range(10))
True

>>> 15 in set(range(10))
False

>>> 5 in {range(10)}
False

>>> 15 in {range(10)}
False

>>> {range(10)}
{range(0, 10)}

>>> set(range(10))
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
------------------------------------------

After a few days of sitting on what seemed at first to be seemingly
buggy, inconsistent set-creation behaviour, I've finally decided it's
not a bug, but a misunderstanding of mine. However, that does still
leave me with one question: why does Python 3.1.1 allow me to get
caught up in my confusion while Python 3.0 (an EARLIER version) spouts
an error pointing me in the right direction? (see below for Python 3.0
behaviour)

----------------------------------------
Python 3.0 (r30:67507, Dec  3 2008, 20:14:27) [MSC v.1500 32 bit
(Intel)] on win32

>>> 5 in set(range(10))
True

>>> 15 in set(range(10))
False

>>> 5 in {range(10)}
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'range'

>>> 15 in {range(10)}
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'range'



More information about the Python-list mailing list