Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Bug in Python set

Reply
Thread Tools

Bug in Python set

 
 
dmitrey
Guest
Posts: n/a
 
      05-02-2010
Python 2.6.5 r265:79063
>>>set().update(set()) is None

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?)

Regards, D.
 
Reply With Quote
 
 
 
 
Aahz
Guest
Posts: n/a
 
      05-02-2010
In article <0bd314a8-db65-43f1-a999->,
dmitrey <> wrote:
>
>Python 2.6.5 r265:79063
>>>>set().update(set()) is None

>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
--
Aahz () <*> http://www.pythoncraft.com/

"It is easier to optimize correct code than to correct optimized code."
--Bill Harlan
 
Reply With Quote
 
 
 
 
Steven D'Aprano
Guest
Posts: n/a
 
      05-02-2010
On Sun, 02 May 2010 05:11:40 -0700, dmitrey wrote:

> Python 2.6.5 r265:79063
>>>>set().update(set()) is None

> 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?)


>>> s = set()
>>> s.add(None)
>>> s

{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?



--
Steven
 
Reply With Quote
 
Terry Reedy
Guest
Posts: n/a
 
      05-02-2010
On 5/2/2010 8:11 AM, dmitrey wrote:
> Python 2.6.5 r265:79063
>>>> set().update(set()) is None

> 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.



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Python 2.7 parser bug on syntax for set literals ? Steve Howe Python 0 10-24-2010 04:40 AM
*bug* *bug* *bug* David Raleigh Arnold Firefox 12 04-02-2007 03:13 AM
mplayer bug or python bug? Marco Python 2 02-07-2007 09:25 PM
-W: Python bug? Documentation bug? Clarence Python 1 12-13-2006 01:43 AM
Re: BUG? OR NOT A BUG? John ASP .Net 2 09-21-2005 10:31 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57