Bug in Python set

Discussion in 'Python' started by dmitrey, May 2, 2010.

  1. dmitrey

    dmitrey Guest

    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.
     
    dmitrey, May 2, 2010
    #1
    1. Advertising

  2. dmitrey

    Aahz Guest

    In article <>,
    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
     
    Aahz, May 2, 2010
    #2
    1. Advertising

  3. 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
     
    Steven D'Aprano, May 2, 2010
    #3
  4. dmitrey

    Terry Reedy Guest

    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.
     
    Terry Reedy, May 2, 2010
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John

    Re: BUG? OR NOT A BUG?

    John, Sep 20, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    587
  2. Clarence

    -W: Python bug? Documentation bug?

    Clarence, Dec 13, 2006, in forum: Python
    Replies:
    1
    Views:
    244
    Clarence
    Dec 13, 2006
  3. Marco

    mplayer bug or python bug?

    Marco, Feb 7, 2007, in forum: Python
    Replies:
    2
    Views:
    375
    Szabolcs Nagy
    Feb 7, 2007
  4. Steve Howe
    Replies:
    0
    Views:
    244
    Steve Howe
    Oct 24, 2010
  5. Replies:
    13
    Views:
    259
    Ned Deily
    Apr 13, 2013
Loading...

Share This Page