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 8 1071
Ciao, Michele:
On Thu, Sep 4, 2008 at 11:48 AM, Michele Petrazzo
<mi**************@togliunipex.itwrote:
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/
On Thu, Sep 4, 2008 at 11:58 AM, Wojtek Walczak <gm*****@bzt.bztwrote:
On Thu, 04 Sep 2008 11:48:18 +0200, 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:
...
>It's possible?
As far as I understand you, you need descriptors: http://users.rcn.com/python/download/Descriptor.htm
I know descriptors a litte, Wojtek, but didn't use them often; can you
elaborate a little more on your idea?
--
Regards,
Wojtek Walczak, http://tosh.pl/gminick/
-- http://mail.python.org/mailman/listinfo/python-list
Saluti
Marco
--
Marco Bizzarri http://notenotturne.blogspot.com/ http://iliveinpisa.blogspot.com/
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
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
>>class foo(set):
.... def __contains__(self, value):
.... print value
....
>>a = foo((1,2))
Thanks,
Michele
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
On Thu, Sep 4, 2008 at 3:07 PM, Maric Michaud <ma***@aristote.infowrote:
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/
On Thu, 4 Sep 2008 12:06:14 +0200, Marco Bizzarri wrote:
>As far as I understand you, you need descriptors: http://users.rcn.com/python/download/Descriptor.htm
I know descriptors a litte, Wojtek, but didn't use them often; can you
elaborate a little more on your idea?
Marco, I think that I misunderstood the OP, but I was thinking
about immutable (or set-once) attributes. Something like this:
---
class SetOnce(object):
def __init__(self):
self._attr1 = ""
self._attr1flag = False
def setatt(self, x):
if self._attr1flag:
raise ValueError("attribute already set")
self._attr1flag = True
self._attr1 = x
def getatt(self):
return self._attr1
attr1 = property(getatt, setatt)
a = SetOnce()
a.attr1 = 1
print a.attr1
try:
a.attr1 = 2
except ValueError:
print a.attr1
---
$ python immutattr.py
1
1
$
--
Regards,
Wojtek Walczak, http://tosh.pl/gminick/ This discussion thread is closed Replies have been disabled for this discussion. Similar topics
5 posts
views
Thread by Gord |
last post: by
|
10 posts
views
Thread by Vishal Grover |
last post: by
|
reply
views
Thread by niko |
last post: by
|
15 posts
views
Thread by Ramaraj M Bijur |
last post: by
|
2 posts
views
Thread by B-Dog |
last post: by
|
3 posts
views
Thread by Developer |
last post: by
|
1 post
views
Thread by Björn Langhof |
last post: by
|
3 posts
views
Thread by Kai Kuehne |
last post: by
|
9 posts
views
Thread by martinezfive |
last post: by
|
2 posts
views
Thread by hzgt9b |
last post: by
| | | | | | | | | | |