The intern() builtin uses this approach:
interned = {}
def intern(s):
if s in interned:
return interned[s]
interned[s] = s
return s
If you've seen it before, and have the old one, return the old one.
Do I have this straight?
Right.
--
Cruxic wrote:
people = set( [Person(1, 'Joe'), Person(2, 'Sue')] )
...
p = people.get_equivalent(2) #method doesn't exist as far as I know
print p.name #prints Sue
def get_equivalent(test, container):
for p in container:
if p == test:
return p
hth,
Alan Isaac
On Mar 7, 11:20 am, Raymond Hettinger [EMAIL PROTECTED] wrote:
[Cruxic]
Is it possible to get an object out of a set() given another object
that has the same hash code and equality (__hash__() and __eq__()
return the same)?
Yes, but it requires an indirect
On Mar 8, 7:32 am, Alan Isaac [EMAIL PROTECTED] wrote:
Cruxic wrote:
people = set( [Person(1, 'Joe'), Person(2, 'Sue')] )
...
p = people.get_equivalent(2) #method doesn't exist as far as I know
print p.name #prints Sue
def get_equivalent(test, container):
for p in container:
Is it possible to get an object out of a set() given another object
that has the same hash code and equality (__hash__() and __eq__()
return the same)?
Yes, but it requires an indirect
approach.http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/499299
Raymond
That's a
something something equivalence class.
The intern() builtin uses this approach:
interned = {}
def intern(s):
if s in interned:
return interned[s]
interned[s] = s
return s
If you've seen it before, and have the old one, return the old one.
Do I
Hello, all.
Is it possible to get an object out of a set() given another object
that has the same hash code and equality (__hash__() and __eq__()
return the same)?
You can't do this with Java Sets either and I've needed it on multiple
occasions. Doesn't it seem like it would be useful?
[Cruxic]
Is it possible to get an object out of a set() given another object
that has the same hash code and equality (__hash__() and __eq__()
return the same)?
Yes, but it requires an indirect approach.
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/499299
Raymond
--