Kyle, I didn't apply the patch you have below. For SAGE-2.5, I did, however, make some changes to how sets and hashing of sets works so that your examples work fine, and I included them in the docstrings. Basically, now finite sets will have their frozenset computed when the set is created.
-- William On 4/13/07, Kyle Schalm <[EMAIL PROTECTED]> wrote: > > > i discovered a bug when i tried to use a Set as a dictionary key. i've > included a patch below. i have also created a test case illustrating the > bug -- in the future, where do i put this in the code/docs so it can go in > the patch too? > > here is the test case: > > --- > In [72]: d={Set([2*I,1+I]):1} > > In [73]: d[Set([1+I,2*I])] > <type 'exceptions.KeyError'> > --- > > note that this bug only occurs when python doesn't know > how to sort the elements in the Set. e.g.: > > In [74]: d={Set([1,2]):1} > > In [75]: d[Set([2,1])] > Out[75]: 1 > > works because Set([1,2]) and Set([2,1]) print the same: > > In [76]: Set([1,2]) > Out[76]: {1, 2} > > In [77]: Set([2,1]) > Out[77]: {1, 2} > > while > > In [78]: Set([2*I,1+I]) > Out[78]: {2.00000000000000*I, 1.00000000000000 + 1.00000000000000*I} > > In [79]: Set([1+I,2*I]) > Out[79]: {1.00000000000000 + 1.00000000000000*I, 2.00000000000000*I} > > print differently. > this in turn led to a different hash value, since the default hash value > comes from the string representation. > --- > > below is the patch. > > > > # HG changeset patch > # User Kyle Schalm <[EMAIL PROTECTED]>> > # Date 1176451637 18000 > # Node ID bca0cc86fd5e88dc21887c5d222c1fcfa71ae490 > # Parent 76e21a785e866569cc43a15b01ee81d9dd131b43 > fix bug when using Set as dictionary key > > diff -r 76e21a785e86 -r bca0cc86fd5e sage/sets/set.py > --- a/sage/sets/set.py Fri Apr 13 00:29:21 2007 -0500 > +++ b/sage/sets/set.py Fri Apr 13 03:07:17 2007 -0500 > @@ -505,7 +505,7 @@ class Set_object_enumerated(Set_object): > > def _repr_(self): > s = str(self.set()) > - return "{" + s[5:-2] + "}" > + return "{" + s[11:-2] + "}" > # return "Finite set of elements of %s"%self.__object > > def set(self): > @@ -530,8 +530,11 @@ class Set_object_enumerated(Set_object): > try: > return self.__set > except AttributeError: > - self.__set = set(self.object()) > + self.__set = frozenset(self.object()) > return self.__set > + > + def __hash__(self): > + return hash(self.set()) > > def __cmp__(self, other): > """ > > > > > -- William Stein Associate Professor of Mathematics University of Washington http://www.williamstein.org --~--~---------~--~----~------------~-------~--~----~ To post to this group, send email to sage-devel@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sage-devel URLs: http://sage.scipy.org/sage/ and http://modular.math.washington.edu/sage/ -~----------~----~----~----~------~----~------~--~---