Set literal confusion

K

kaoruAngel

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
True
False
False
False
{range(0, 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
True
False
Traceback (most recent call last):
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'range'
 
P

Peter Otten

kaoruAngel said:
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
True
False
False
False
{range(0, 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
True
False
Traceback (most recent call last):
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(0, 10)}

Peter
 
V

Vlastimil Brom

2009/9/22 kaoruAngel said:
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
True
False
False
False
{range(0, 10)}
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
You might also try the set comprehension:

Python 3.1 (r31:73574, Jun 26 2009, 20:21:35) [MSC v.1500 32 bit
(Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
For these simple cases without conditions, it probably doesn't make
much difference.
The character of the single items is maybe more obvious.

vbr
 
D

Dennis Lee Bieber

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.
Read the changes between 2.x and 3.x

range() is what used to be xrange() -- an on-demand generator of
values. You created a set containing a single generator. Try something
like:

5 in {list(range(10))}
 
P

Peter Otten

Dennis said:
range() is what used to be xrange() -- an on-demand generator of
values. You created a set containing a single generator. Try something
like:

5 in {list(range(10))}

No. {expr} is always a set with a single element.
{range(0, 10)}

That element cannot be a list, by the way, because lists aren't hashable.
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'

Peter
 
D

Dennis Lee Bieber

No. {expr} is always a set with a single element.
Accepted -- shows how often I use sets in my small jobs...

The main thing I was trying to implement was something that would
consume the generator leaving the values... Which probably means (again,
my set-less experience shows)

5 in set(range(10))

is the proper one <G>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,782
Latest member
ThomasGex

Latest Threads

Top