Bug in Python set

D

dmitrey

Python 2.6.5 r265:79063True
while I expect result of update to be set.
Also, result of
set().add(None)
is None while I expect it to be set with element None (or, maybe, it
should be empty set?)

Regards, D.
 
A

Aahz

Python 2.6.5 r265:79063
True
while I expect result of update to be set.
Also, result of
set().add(None)
is None while I expect it to be set with element None (or, maybe, it
should be empty set?)

Why are you assuming that your expectations are correct? Generally
speaking, Python methods that mutate do *not* return the original object,
precisely to make sure you don't make stupid mistakes.

You should probably read this:

http://www.catb.org/~esr/faqs/smart-questions.html
 
S

Steven D'Aprano

Python 2.6.5 r265:79063
True
while I expect result of update to be set.

Change your expectations. Generally, methods which modify the object
rather than creating a new one return None.
s = set([1,2,3])
s.update(set([3, 4, 5]))
s
{1, 2, 3, 4, 5}

Also, result of set().add(None) is None while I expect it to be set
with element None (or, maybe, it should be empty set?)
{None}


Python sets have been used by tens of thousands of programmers for many
years now. Which do you think is more likely?

(1) Not one person before you noticed that something as fundamental as
adding an item to a set is buggy;

or

(2) You have misunderstood what is happening?
 
T

Terry Reedy

Python 2.6.5 r265:79063
True
while I expect result of update to be set.
Also, result of
set().add(None)
is None while I expect it to be set with element None (or, maybe, it
should be empty set?)

'Expect' has two different meanings in this context.
1. The empirical behavior surprised me (because I did not bother to read
the manual, which clearly says what the returns are).
2. The documented behavior, which I read, surprises me, because I would
have designed things differently, perhaps because I have used other
languages designed differently.

I am not sure which you meant.
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top