optimizing large dictionaries

Matthias Julius jnews at julius-net.net
Sat Jan 17 00:47:47 CET 2009

Per Freem <perfreem at yahoo.com> writes:

> the only 'twist' is that my elt is an instance of a class (MyClass)
> with 3 fields, all numeric. the class is hashable, and so
> my_dict[elt] works well.  the __repr__ and __hash__ methods of my
> class simply return str() representation of self, 

which just calls __str__().  I guess you are aware of that but you
could call self.__str__() directly.  Maybe that saves something when
you do that 10 million times.

> while __str__ just makes everything numeric field into a
> concatenated string:
> class MyClass
>   def __str__(self):
>     return "%s-%s-%s" %(self.field1, self.field2, self.field3)
>   def __repr__(self):
>     return str(self)
>   def __hash__(self):
>     return hash(str(self))

Maybe it would be faster to numerically combine the three fields
instead of hashing the string representation.


More information about the Python-list mailing list