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?
Using a decorator/delegate-pattern, yes:
class Foo(object):
def __init__(self, delegate):
self.delegate = delegate
def __hash__(self):
return hash(self.delegate.attr)
def __cmp__(self, other):
return cmp(self.delegate.attr, other.delegate.attr)
set(Foo(a) for a in bar1, bar2)
Diez
--
http://mail.python.org/mailman/listinfo/python-list