Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

(1) Ah. Here capitalization matters, so that "a sequence" refers to sequences in general, and both lists and tuples are sequences. "Sequence" refers to the object you're asking about named "Sequence". kcrisman answered the general case, which is what I thought you were asking too.

(2) If I understand you (and I'm not sure that I do), then his answer still applies: you make a new object which is mutable. In Python mutability isn't usually considered a switch you can toggle but a feature of types; for practical reasons Sage makes it easier to freeze objects, but often to "undo" this you need to make a new object.

sage: v = Sequence(range(5), universe=ZZ)
sage: v
[0, 1, 2, 3, 4]
sage: v.is_mutable()
True
sage: v[2] = 3
sage: v
[0, 1, 3, 3, 4]
sage: 
sage: # make it immutable
sage: v.set_immutable()
sage: v.is_mutable()
False
sage: v[3] = 8
---------------------------------------------------------------------------
[.. deletia]
ValueError: object is immutable; please change a copy instead.
sage: # make mutable again
sage: v = Sequence(v) # or seq(v), that works too
sage: v
[0, 1, 3, 3, 4]
sage: v.is_mutable()
True
sage: v[3] = 8
sage: v
[0, 1, 3, 8, 4]

That's what the ValueError message is telling you to do: work with a new copy..

(1) Ah. Here capitalization matters, so that "a sequence" refers to sequences in general, and both lists and tuples are sequences. "Sequence" refers to the object you're asking about named "Sequence". kcrisman answered the general case, which is what I thought you were asking too.

(2) If I understand you (and I'm not sure that I do), then his answer still applies: you make a new object which is mutable. In Python mutability isn't usually considered a switch you can toggle but a feature of types; for practical reasons Sage makes it easier to freeze objects, but often to "undo" this you need to make a new object.

sage: v = Sequence(range(5), universe=ZZ)
sage: v
[0, 1, 2, 3, 4]
sage: v.is_mutable()
True
sage: v[2] = 3
sage: v
[0, 1, 3, 3, 4]
sage: 
sage: # make it immutable
sage: v.set_immutable()
sage: v.is_mutable()
False
sage: v[3] = 8
---------------------------------------------------------------------------
[.. deletia]
ValueError: object is immutable; please change a copy instead.
sage: # make mutable again
sage: v = Sequence(v) # or seq(v), that works too
sage: v
[0, 1, 3, 3, 4]
sage: v.is_mutable()
True
sage: v[3] = 8
sage: v
[0, 1, 3, 8, 4]

That's what the ValueError message is telling you to do: work with a new copy..copy.