Object Integer mapping

NighterNet darkneter at gmail.com
Wed Jan 20 11:43:38 EST 2010


Need help on python version 3.1.x. I can't seem to know what going on
a bit. The code that I check that the hash is different every time. Is
there a way to make the hash the same? I using as to check the class
is the same variables but if it different variable in the class that
it add to the array and return the index.


# Generic Object->Integer mapping
# the object must be usable as a dictionary key
class ObjMap:
	def __init__(self):
		self.dict = {}
		self.next = 0
	def get(self, obj):
		if obj in self.dict:
			return self.dict[obj]
		else:
			id = self.next
			self.next = self.next + 1
			self.dict[obj] = id
			return id

	def items(self):
		getval = operator.itemgetter(0)
		getkey = operator.itemgetter(1)
		return map(getval, sorted(self.dict.items(), key=getkey))


class Point:
	def __init__(self):
		self.x = 0
		self.y = 0
		self.z = 0

points = ObjMap()

Testme = Point()
Testme.x = 0
print(points.get(Testme))
Testme2 = Point()
Testme2.y = 1
print(points.get(Testme2))
Testme3 = Point()
Testme3.y = 1
print(points.get(Testme3))
Ttestme4 = Point()
Ttestme4.y = 1
print( points.get(Ttestme4))

It keep adding new array from this but doesn't match hash. Need help
on how to fix this class code.



More information about the Python-list mailing list