On 4/13/07, Kyle Schalm <[EMAIL PROTECTED]> wrote: > while i'm on the topic of sets, is the Set class intended to be immutable?
No. The builtin Python set type isn't immutable, so it doesn't seem sensible to make the SAGE one immutable. E.g., the builtin set type has an add method: > sage: a = set([1,2,3]) > sage: a.add(10) > sage: a > set([1, 2, 3, 10]) Since sets aren't immutable, they aren't hashable: > sage: hash(a) >> <type 'exceptions.TypeError'>: set objects are unhashable Sets in SAGE should have very similar behavior to Python sets, except "be more mathematical", and of course allow for infinite sets. Thus actually, regarding your previous patch, sets shouldn't be allowed as dictionary keys and shouldn't have a hash method at all, or have one that raises type error. If you want to use one as a key, you should maybe be using a tuple instead. In short -- SAGE sets should be like Python sets, unless there is a very very good argument otherwise, and Python sets are mutable and not hashable. I would not be opposed to creating an immutability flag for sets, and having a "set_immutable" option (like with Sequences), to create immutable sets. But those shouldn't be the default. > this does seem to be the intent, since there is no method on the object > itself which can be used to change its value. however, > > In [58]: x=[1,2,3] > > In [59]: s=Set(x) > > In [60]: x[1]=5 > > In [61]: s > Out[61]: {1, 3, 5} > > > i propose that this is undesirable, and there is an easy fix, assuming > any X for which X.is_finite() == True also has an iterator (and can > therefore be converted to a python set). see patch below. > note: the second part of this patch assumes the patch in my previous email > has been applied. if not, just use the first part of this patch. > > > > # HG changeset patch > # User Kyle Schalm <[EMAIL PROTECTED]>> > # Date 1176453082 18000 > # Node ID ce41e74d58b655aa854acba1e08a69fec9534cc6 > # Parent bca0cc86fd5e88dc21887c5d222c1fcfa71ae490 > guard Set_object_enumerated against accidental change via underlying > object > > diff -r bca0cc86fd5e -r ce41e74d58b6 sage/sets/set.py > --- a/sage/sets/set.py Fri Apr 13 03:07:17 2007 -0500 > +++ b/sage/sets/set.py Fri Apr 13 03:31:22 2007 -0500 > @@ -55,8 +55,8 @@ def Set(X): > if isinstance(X, Element): > raise TypeError, "Element has no defined underlying set" > try: > - if isinstance(X, (list, tuple, set)) or X.is_finite(): > - return Set_object_enumerated(X) > + if isinstance(X, (list, tuple, set, frozenset)) or X.is_finite(): > + return Set_object_enumerated(frozenset(X)) > except AttributeError: > pass > return Set_object(X) > @@ -527,11 +527,7 @@ class Set_object_enumerated(Set_object): > sage: type(X) > <class 'sage.sets.set.Set_object_enumerated'> > """ > - try: > - return self.__set > - except AttributeError: > - self.__set = frozenset(self.object()) > - return self.__set > + return self.object() > > def __hash__(self): > return hash(self.set()) > > > > > > -- William Stein Associate Professor of Mathematics University of Washington http://www.williamstein.org --~--~---------~--~----~------------~-------~--~----~ To post to this group, send email to [EMAIL PROTECTED] 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/ -~----------~----~----~----~------~----~------~--~---