Set literal confusion
Peter Otten
__peter__ at web.de
Tue Sep 22 13:20:12 EDT 2009
kaoruAngel wrote:
> 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)
That is probably a bug: http://bugs.python.org/issue4701
> ----------------------------------------
> 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'
My younger 3.0.1 doesn't show that behaviour:
Python 3.0.1+ (r301:69556, Apr 15 2009, 17:25:52)
[GCC 4.3.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> {range(10)}
{range(0, 10)}
Peter
More information about the Python-list
mailing list