Thanks for the detailed explanation! If I understood correctly, you suggest that I remove __copy__() from the derived class PowerSeries_poly, and leave in place the __copy__ of PowerSeries that just returns self. This would do the correct thing for the currently unique derived class, and if someone eventually implements PowerSeries_mutable, they can override __copy__ with the appropriate thing.
Alex On Mon, Jul 13, 2009 at 10:23 AM, William Stein<wst...@gmail.com> wrote: > There are a few questions here. > > 1. If all instances of a class C are immutable, is reasonable for > __copy__ to just return self? > > 2. Are all instances of PowerSeries going to be immutable? Or, are we > someday going to have a class PowerSeries_mutable? > > > Regarding 1, fortunately Python itself provides some help with this question. > > sage: import copy > sage: a = int(903840823) > sage: copy.copy(a) is a > True > sage: a = float(2.39494) > sage: copy.copy(a) is a > True > sage: a = 'hi there' > sage: copy.copy(a) is a > True > > I.e., the answer to question 1 is a resounding "yes!". > > Regarding question 2, note that power series are already secretly mutable: > > sage: R.<x> = QQ[] > sage: f = x^3 + 3*x - 1 > sage: f._unsafe_mutate(1, 10) > sage: f > x^3 + 10*x - 1 > sage: R.<x> = QQ[[]] > sage: f = x^3 + 3*x - 1 + O(x^5) > sage: f._unsafe_mutate(1, 10); f > -1 + 10*x + x^3 + O(x^5) > > The function _unsafe_mutate is there because long ago polynomials and > power series were trivially mutable, i.e., this used to work: > > sage: f[1] = 1100 > > and David Harvey implemented some nice code that used this. Then in > late 2006 we went through and made many basic objects immutable in > Sage, since mutable objects should *never* be hashable, or many basic > Python algorithms break. All parents were made immutable, as were a > wide range of elements (with the notable exception of matrices and > vectors, which can now be both immutable or mutable). > > Anyway, when changing things to immutable, I didn't want to break > David's fine code, so _unsafe_mutate -- whose unsafeness should be > clear -- was introduced. > > The upshot of all this is that one almost always really wants > polynomials and power series to be immutable, in which case __copy__ > returning self is perfectly fine. Sometimes mutating > polynomials/powerseries is very useful for certain algorithms though. > > So I would say the answer to 2 is a not resounding "yes" for now, with > the caveat that one can simply do a little code refactoring later of > mutable polynomials and power series are introduced. Given that I've > not seen a single remark on any Sage list about > mutability/immutability of power series and polynomials during the > last 3 years, I doubt that the lack of mutability being fully > supported (like it is for matrices) is a serious problem. > > -- William > -- Alex Ghitza -- Lecturer in Mathematics -- The University of Melbourne -- Australia -- http://www.ms.unimelb.edu.au/~aghitza/ --~--~---------~--~----~------------~-------~--~----~ To post to this group, send email to sage-devel@googlegroups.com To unsubscribe from this group, send email to sage-devel-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sage-devel URLs: http://www.sagemath.org -~----------~----~----~----~------~----~------~--~---