Get item from set

Johannes Bauer dfnsonfsduifb at gmx.de
Sun Apr 26 07:41:56 EDT 2009


Hi group,

I have a very simple about sets. This is a minimal example:

#!/usr/bin/python
class x():
	def __init__(self, y):
		self.__y = y
	def __eq__(self, other):
		return self.__y == other.__y
	def __hash__(self):
		return hash(self.__y)

a = x("foo")
s = set([x("bar"), x("moo"), a])
z = x("foo")
print("z = ", z)
print(s)
for i in s:
	print(i, i == a, i is a, i == z, i is z)

The problem is: two instances of x() are equal (__eq__ returns true),
but they are not identical. I have an equal element ("z"), but want to
get the *actual* element ("a") in the set. I.d. in the above example,
i'd like something like:

print(s.getelement(z) is a)
True

Is there something like the "getelement" function? How can I do what I want?

Kind regards,
Johannes

-- 
"Meine Gegenklage gegen dich lautet dann auf bewusste Verlogenheit,
verlästerung von Gott, Bibel und mir und bewusster Blasphemie."
         -- Prophet und Visionär Hans Joss aka HJP in de.sci.physik
                         <48d8bf1d$0$7510$5402220f at news.sunrise.ch>



More information about the Python-list mailing list