C
Cruxic
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? Consider:
class Person:
def __init__(self, id, name):
self.id = id
self.name = name
def __hash__(self):
return self.id
def __eq__(self, other):
return self.id == other.id
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
I'm not sure if the above code compiles but I hope you get the idea.
Is it possible?
Much Thanks.
- 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)?
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? Consider:
class Person:
def __init__(self, id, name):
self.id = id
self.name = name
def __hash__(self):
return self.id
def __eq__(self, other):
return self.id == other.id
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
I'm not sure if the above code compiles but I hope you get the idea.
Is it possible?
Much Thanks.
- Cruxic