Re: overwrite set behavior

Discussion in 'Python' started by Marco Bizzarri, Sep 4, 2008.

  1. Ciao, Michele:

    On Thu, Sep 4, 2008 at 11:48 AM, Michele Petrazzo
    <> wrote:
    > Hi all, I want to modify the method that set use for see if there is
    > already an object inside its obj-list. Something like this:
    >
    > class foo: pass
    >
    > bar1 = foo()
    > bar1.attr = 1
    >
    > bar2 = foo()
    > bar2.attr = 1
    >
    > set( (bar1, bar2), key=lambda o: o.attr)
    >
    > and, of course, set has only one value.
    >
    > It's possible?
    >
    > Thanks,
    > Michele
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >


    looking at the source, maybe you could create a subclass of Set
    redefining the __contains__ method?

    Regards
    Marco


    --
    Marco Bizzarri
    http://notenotturne.blogspot.com/
    http://iliveinpisa.blogspot.com/
     
    Marco Bizzarri, Sep 4, 2008
    #1
    1. Advertising

  2. Le Thursday 04 September 2008 14:31:23 Michele Petrazzo, vous avez écrit :
    > Marco Bizzarri wrote:
    > > looking at the source, maybe you could create a subclass of Set
    > > redefining the __contains__ method?

    >
    > Made some tries, but __contains__ are never called
    >


    No, __contains__ is only called with "in" operator, not for internal hashing.
    Anyway this solution is bad, you'll need to compare the new element with all
    the set contain, which would result in a O(n) algorithm for adding elements
    to the set in place of the O(1) it use.

    The right way to go is one like Diez show you in a previous post.

    >  >>> class foo(set):
    > ...  def __contains__(self, value):
    > ...   print value
    > ...
    >  >>> a = foo((1,2))
    >  >>>




    --
    _____________

    Maric Michaud
     
    Maric Michaud, Sep 4, 2008
    #2
    1. Advertising

  3. On Thu, Sep 4, 2008 at 3:07 PM, Maric Michaud <> wrote:
    > Le Thursday 04 September 2008 14:31:23 Michele Petrazzo, vous avez écrit :
    >> Marco Bizzarri wrote:
    >> > looking at the source, maybe you could create a subclass of Set
    >> > redefining the __contains__ method?

    >>
    >> Made some tries, but __contains__ are never called
    >>

    >
    > No, __contains__ is only called with "in" operator, not for internal hashing.
    > Anyway this solution is bad, you'll need to compare the new element with all
    > the set contain, which would result in a O(n) algorithm for adding elements
    > to the set in place of the O(1) it use.
    >


    Thanks for the clarification, Maric; I take notices to watch source
    more closely next time (( hopefully, before writing a wrong answer )).

    Regards
    Marco

    > _____________
    >
    > Maric Michaud
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >




    --
    Marco Bizzarri
    http://notenotturne.blogspot.com/
    http://iliveinpisa.blogspot.com/
     
    Marco Bizzarri, Sep 4, 2008
    #3
    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. =?Utf-8?B?Um9iZXJ0?=

    Deployment in 2.0 - How To Not Overwrite web.config

    =?Utf-8?B?Um9iZXJ0?=, Nov 2, 2005, in forum: ASP .Net
    Replies:
    8
    Views:
    4,522
    Juan T. Llibre
    Nov 3, 2005
  2. VisionSet
    Replies:
    1
    Views:
    4,207
    Adam Lipscombe
    Sep 2, 2003
  3. zalla rouge
    Replies:
    0
    Views:
    484
    zalla rouge
    Sep 2, 2003
  4. shruti tiwari via JavaKB.com

    overwrite existing value of a textbox with null

    shruti tiwari via JavaKB.com, Apr 2, 2005, in forum: Java
    Replies:
    1
    Views:
    683
    David.Federman
    Apr 3, 2005
  5. Philipp Leitner

    Overwrite Default Constructor ?

    Philipp Leitner, Apr 17, 2006, in forum: Java
    Replies:
    26
    Views:
    4,452
    Jeroen V.
    Apr 19, 2006
Loading...

Share This Page