ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Fri, 07 Jan 2011 17:08:16 -0600How to set mutable an inmutable sequence.http://ask.sagemath.org/question/7783/how-to-set-mutable-an-inmutable-sequence/I think the title is quite descriptive.
I would like to know, how can you set mutable an inmutable sequence. Is that even possible?
I would be very grateful if you can link me the file of the sage documentation where I can find that info.
Thanks.Sun, 02 Jan 2011 16:12:45 -0600http://ask.sagemath.org/question/7783/how-to-set-mutable-an-inmutable-sequence/Answer by Tlk for <p>I think the title is quite descriptive.</p>
<p>I would like to know, how can you set mutable an inmutable sequence. Is that even possible?</p>
<p>I would be very grateful if you can link me the file of the sage documentation where I can find that info.</p>
<p>Thanks.</p>
http://ask.sagemath.org/question/7783/how-to-set-mutable-an-inmutable-sequence/?answer=11918#post-id-11918That's not a sequence.
seq = (1,2,3,4) <-- tuple
sage: seq2 = list(seq) <-- list
I mean how to do mutable an inmutable **sequence** (http://www.sagemath.org/doc/reference/sage/structure/sequence.html) like, for example:
v = Sequence(range(5), universe=ZZ)
Mon, 03 Jan 2011 08:20:57 -0600http://ask.sagemath.org/question/7783/how-to-set-mutable-an-inmutable-sequence/?answer=11918#post-id-11918Comment by kcrisman for <p>That's not a sequence. </p>
<p>seq = (1,2,3,4) <-- tuple
sage: seq2 = list(seq) <-- list</p>
<p>I mean how to do mutable an inmutable <strong>sequence</strong>(<a href="http://www.sagemath.org/doc/reference/sage/structure/sequence.html">http://www.sagemath.org/doc/reference...</a>) like, for example:
v = Sequence(range(5), universe=ZZ)</p>
http://ask.sagemath.org/question/7783/how-to-set-mutable-an-inmutable-sequence/?comment=22346#post-id-22346Thanks for the clarification (see DSM's reply). By the way, you should be able to edit your original question without the need to create an 'answer' yourself - hopefully even with not much 'karma'!Mon, 03 Jan 2011 14:10:38 -0600http://ask.sagemath.org/question/7783/how-to-set-mutable-an-inmutable-sequence/?comment=22346#post-id-22346Answer by DSM for <p>I think the title is quite descriptive.</p>
<p>I would like to know, how can you set mutable an inmutable sequence. Is that even possible?</p>
<p>I would be very grateful if you can link me the file of the sage documentation where I can find that info.</p>
<p>Thanks.</p>
http://ask.sagemath.org/question/7783/how-to-set-mutable-an-inmutable-sequence/?answer=11919#post-id-11919(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 <i>is</i> 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.
<pre><code>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]
</code></pre>
That's what the ValueError message is telling you to do: work with a new copy.
Mon, 03 Jan 2011 13:13:25 -0600http://ask.sagemath.org/question/7783/how-to-set-mutable-an-inmutable-sequence/?answer=11919#post-id-11919Comment by Tlk for <p>(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.</p>
<p>(2) If I understand you (and I'm not sure that I do), then his answer still applies: you make a new object which <i>is</i> 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.</p>
<pre><code>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]
</code></pre>
<p>That's what the ValueError message is telling you to do: work with a new copy.</p>
http://ask.sagemath.org/question/7783/how-to-set-mutable-an-inmutable-sequence/?comment=22341#post-id-22341Okey, thank you for the answer. Sorry about the Sequence staff, I guess that's because of my english vocabularyMon, 03 Jan 2011 19:19:36 -0600http://ask.sagemath.org/question/7783/how-to-set-mutable-an-inmutable-sequence/?comment=22341#post-id-22341Answer by DSM for <p>I think the title is quite descriptive.</p>
<p>I would like to know, how can you set mutable an inmutable sequence. Is that even possible?</p>
<p>I would be very grateful if you can link me the file of the sage documentation where I can find that info.</p>
<p>Thanks.</p>
http://ask.sagemath.org/question/7783/how-to-set-mutable-an-inmutable-sequence/?answer=11932#post-id-11932Too long for a comment. Playing games with the mutability isn't a solution, it's a problem waiting to happen!
Tik, if you're going to do this, it would be "v._is_immutable = False", with "is" instead of "set", two ms, and different capitalization on False: remember that Python is case-sensitive. (Think "impossible" to get the spelling right, and don't blame me for the inconsistency. :^) I'd also be quite surprised if this were documented: single-underscore variables are usually considered private, and you often don't even document changes in them beween versions.
But bypassing the interface and changing the internal state directly is a bad idea -- if someone provides a setter they probably did it for a reason -- and a way to cause hard-to-detect bugs, because then you can make all sorts of mistakes that the immutability was trying to prevent you from making.
<pre><code>
sage: u = Sequence([0,1,2,3,5])
sage: v = Sequence([0,1,2,3,4])
sage: hash(u)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
[...]
ValueError: immutable sequences are unhashable
sage: # [Hey, there's a typo: this should be "<i>mutable</i> sequences are unhashable".
sage: # Anyone with commit privs, please fix.]
sage: u.set_immutable()
sage: v.set_immutable()
sage: hash(u)
-3958796579501393844
sage:
sage: d = {u: 3, v: 4} # since they're immutable we can hash them, and use them as dictionary keys
sage: d
{[0, 1, 2, 3, 5]: 3, [0, 1, 2, 3, 4]: 4}
sage:
sage: u[4] = 4
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
[...]
ValueError: object is immutable; please change a copy instead.
sage: u._is_immutable = False # don't do this!
sage: u[4] = 4
sage: d
{[0, 1, 2, 3, 4]: 3, [0, 1, 2, 3, 4]: 4}
sage: # what?!
</code></pre>
With Python people usually don't go out of their way to prevent you from making mistakes the way they do in (e.g.) C++, and so all sorts of things are possible. But with this power comes responsibility, namely the responsibility not to shoot yourself in the foot when there's no need to.Wed, 05 Jan 2011 13:37:20 -0600http://ask.sagemath.org/question/7783/how-to-set-mutable-an-inmutable-sequence/?answer=11932#post-id-11932Comment by kcrisman for <p>Too long for a comment. Playing games with the mutability isn't a solution, it's a problem waiting to happen!</p>
<p>Tik, if you're going to do this, it would be "v._is_immutable = False", with "is" instead of "set", two ms, and different capitalization on False: remember that Python is case-sensitive. (Think "impossible" to get the spelling right, and don't blame me for the inconsistency. :^) I'd also be quite surprised if this were documented: single-underscore variables are usually considered private, and you often don't even document changes in them beween versions.</p>
<p>But bypassing the interface and changing the internal state directly is a bad idea -- if someone provides a setter they probably did it for a reason -- and a way to cause hard-to-detect bugs, because then you can make all sorts of mistakes that the immutability was trying to prevent you from making.</p>
<pre><code>
sage: u = Sequence([0,1,2,3,5])
sage: v = Sequence([0,1,2,3,4])
sage: hash(u)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
[...]
ValueError: immutable sequences are unhashable
sage: # [Hey, there's a typo: this should be "<i>mutable</i> sequences are unhashable".
sage: # Anyone with commit privs, please fix.]
sage: u.set_immutable()
sage: v.set_immutable()
sage: hash(u)
-3958796579501393844
sage:
sage: d = {u: 3, v: 4} # since they're immutable we can hash them, and use them as dictionary keys
sage: d
{[0, 1, 2, 3, 5]: 3, [0, 1, 2, 3, 4]: 4}
sage:
sage: u[4] = 4
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
[...]
ValueError: object is immutable; please change a copy instead.
sage: u._is_immutable = False # don't do this!
sage: u[4] = 4
sage: d
{[0, 1, 2, 3, 4]: 3, [0, 1, 2, 3, 4]: 4}
sage: # what?!
</code></pre>
<p>With Python people usually don't go out of their way to prevent you from making mistakes the way they do in (e.g.) C++, and so all sorts of things are possible. But with this power comes responsibility, namely the responsibility not to shoot yourself in the foot when there's no need to.</p>
http://ask.sagemath.org/question/7783/how-to-set-mutable-an-inmutable-sequence/?comment=22317#post-id-22317Look in the source of structure.mutability.pyx and structure.mutability_py.py. And of course structure.sequence.py.Fri, 07 Jan 2011 17:08:16 -0600http://ask.sagemath.org/question/7783/how-to-set-mutable-an-inmutable-sequence/?comment=22317#post-id-22317Comment by Tlk for <p>Too long for a comment. Playing games with the mutability isn't a solution, it's a problem waiting to happen!</p>
<p>Tik, if you're going to do this, it would be "v._is_immutable = False", with "is" instead of "set", two ms, and different capitalization on False: remember that Python is case-sensitive. (Think "impossible" to get the spelling right, and don't blame me for the inconsistency. :^) I'd also be quite surprised if this were documented: single-underscore variables are usually considered private, and you often don't even document changes in them beween versions.</p>
<p>But bypassing the interface and changing the internal state directly is a bad idea -- if someone provides a setter they probably did it for a reason -- and a way to cause hard-to-detect bugs, because then you can make all sorts of mistakes that the immutability was trying to prevent you from making.</p>
<pre><code>
sage: u = Sequence([0,1,2,3,5])
sage: v = Sequence([0,1,2,3,4])
sage: hash(u)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
[...]
ValueError: immutable sequences are unhashable
sage: # [Hey, there's a typo: this should be "<i>mutable</i> sequences are unhashable".
sage: # Anyone with commit privs, please fix.]
sage: u.set_immutable()
sage: v.set_immutable()
sage: hash(u)
-3958796579501393844
sage:
sage: d = {u: 3, v: 4} # since they're immutable we can hash them, and use them as dictionary keys
sage: d
{[0, 1, 2, 3, 5]: 3, [0, 1, 2, 3, 4]: 4}
sage:
sage: u[4] = 4
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
[...]
ValueError: object is immutable; please change a copy instead.
sage: u._is_immutable = False # don't do this!
sage: u[4] = 4
sage: d
{[0, 1, 2, 3, 4]: 3, [0, 1, 2, 3, 4]: 4}
sage: # what?!
</code></pre>
<p>With Python people usually don't go out of their way to prevent you from making mistakes the way they do in (e.g.) C++, and so all sorts of things are possible. But with this power comes responsibility, namely the responsibility not to shoot yourself in the foot when there's no need to.</p>
http://ask.sagemath.org/question/7783/how-to-set-mutable-an-inmutable-sequence/?comment=22324#post-id-22324Thanks for the explanation. There is nothing about "_is_immutable" on the doc?Thu, 06 Jan 2011 03:47:11 -0600http://ask.sagemath.org/question/7783/how-to-set-mutable-an-inmutable-sequence/?comment=22324#post-id-22324Comment by kcrisman for <p>Too long for a comment. Playing games with the mutability isn't a solution, it's a problem waiting to happen!</p>
<p>Tik, if you're going to do this, it would be "v._is_immutable = False", with "is" instead of "set", two ms, and different capitalization on False: remember that Python is case-sensitive. (Think "impossible" to get the spelling right, and don't blame me for the inconsistency. :^) I'd also be quite surprised if this were documented: single-underscore variables are usually considered private, and you often don't even document changes in them beween versions.</p>
<p>But bypassing the interface and changing the internal state directly is a bad idea -- if someone provides a setter they probably did it for a reason -- and a way to cause hard-to-detect bugs, because then you can make all sorts of mistakes that the immutability was trying to prevent you from making.</p>
<pre><code>
sage: u = Sequence([0,1,2,3,5])
sage: v = Sequence([0,1,2,3,4])
sage: hash(u)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
[...]
ValueError: immutable sequences are unhashable
sage: # [Hey, there's a typo: this should be "<i>mutable</i> sequences are unhashable".
sage: # Anyone with commit privs, please fix.]
sage: u.set_immutable()
sage: v.set_immutable()
sage: hash(u)
-3958796579501393844
sage:
sage: d = {u: 3, v: 4} # since they're immutable we can hash them, and use them as dictionary keys
sage: d
{[0, 1, 2, 3, 5]: 3, [0, 1, 2, 3, 4]: 4}
sage:
sage: u[4] = 4
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
[...]
ValueError: object is immutable; please change a copy instead.
sage: u._is_immutable = False # don't do this!
sage: u[4] = 4
sage: d
{[0, 1, 2, 3, 4]: 3, [0, 1, 2, 3, 4]: 4}
sage: # what?!
</code></pre>
<p>With Python people usually don't go out of their way to prevent you from making mistakes the way they do in (e.g.) C++, and so all sorts of things are possible. But with this power comes responsibility, namely the responsibility not to shoot yourself in the foot when there's no need to.</p>
http://ask.sagemath.org/question/7783/how-to-set-mutable-an-inmutable-sequence/?comment=22323#post-id-22323Probably this is an internal Python thing?Thu, 06 Jan 2011 15:30:20 -0600http://ask.sagemath.org/question/7783/how-to-set-mutable-an-inmutable-sequence/?comment=22323#post-id-22323Comment by Tlk for <p>Too long for a comment. Playing games with the mutability isn't a solution, it's a problem waiting to happen!</p>
<p>Tik, if you're going to do this, it would be "v._is_immutable = False", with "is" instead of "set", two ms, and different capitalization on False: remember that Python is case-sensitive. (Think "impossible" to get the spelling right, and don't blame me for the inconsistency. :^) I'd also be quite surprised if this were documented: single-underscore variables are usually considered private, and you often don't even document changes in them beween versions.</p>
<p>But bypassing the interface and changing the internal state directly is a bad idea -- if someone provides a setter they probably did it for a reason -- and a way to cause hard-to-detect bugs, because then you can make all sorts of mistakes that the immutability was trying to prevent you from making.</p>
<pre><code>
sage: u = Sequence([0,1,2,3,5])
sage: v = Sequence([0,1,2,3,4])
sage: hash(u)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
[...]
ValueError: immutable sequences are unhashable
sage: # [Hey, there's a typo: this should be "<i>mutable</i> sequences are unhashable".
sage: # Anyone with commit privs, please fix.]
sage: u.set_immutable()
sage: v.set_immutable()
sage: hash(u)
-3958796579501393844
sage:
sage: d = {u: 3, v: 4} # since they're immutable we can hash them, and use them as dictionary keys
sage: d
{[0, 1, 2, 3, 5]: 3, [0, 1, 2, 3, 4]: 4}
sage:
sage: u[4] = 4
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
[...]
ValueError: object is immutable; please change a copy instead.
sage: u._is_immutable = False # don't do this!
sage: u[4] = 4
sage: d
{[0, 1, 2, 3, 4]: 3, [0, 1, 2, 3, 4]: 4}
sage: # what?!
</code></pre>
<p>With Python people usually don't go out of their way to prevent you from making mistakes the way they do in (e.g.) C++, and so all sorts of things are possible. But with this power comes responsibility, namely the responsibility not to shoot yourself in the foot when there's no need to.</p>
http://ask.sagemath.org/question/7783/how-to-set-mutable-an-inmutable-sequence/?comment=22319#post-id-22319I've been trying but it's really hard to find something about that on the doc :SThu, 06 Jan 2011 21:21:30 -0600http://ask.sagemath.org/question/7783/how-to-set-mutable-an-inmutable-sequence/?comment=22319#post-id-22319Comment by DSM for <p>Too long for a comment. Playing games with the mutability isn't a solution, it's a problem waiting to happen!</p>
<p>Tik, if you're going to do this, it would be "v._is_immutable = False", with "is" instead of "set", two ms, and different capitalization on False: remember that Python is case-sensitive. (Think "impossible" to get the spelling right, and don't blame me for the inconsistency. :^) I'd also be quite surprised if this were documented: single-underscore variables are usually considered private, and you often don't even document changes in them beween versions.</p>
<p>But bypassing the interface and changing the internal state directly is a bad idea -- if someone provides a setter they probably did it for a reason -- and a way to cause hard-to-detect bugs, because then you can make all sorts of mistakes that the immutability was trying to prevent you from making.</p>
<pre><code>
sage: u = Sequence([0,1,2,3,5])
sage: v = Sequence([0,1,2,3,4])
sage: hash(u)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
[...]
ValueError: immutable sequences are unhashable
sage: # [Hey, there's a typo: this should be "<i>mutable</i> sequences are unhashable".
sage: # Anyone with commit privs, please fix.]
sage: u.set_immutable()
sage: v.set_immutable()
sage: hash(u)
-3958796579501393844
sage:
sage: d = {u: 3, v: 4} # since they're immutable we can hash them, and use them as dictionary keys
sage: d
{[0, 1, 2, 3, 5]: 3, [0, 1, 2, 3, 4]: 4}
sage:
sage: u[4] = 4
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
[...]
ValueError: object is immutable; please change a copy instead.
sage: u._is_immutable = False # don't do this!
sage: u[4] = 4
sage: d
{[0, 1, 2, 3, 4]: 3, [0, 1, 2, 3, 4]: 4}
sage: # what?!
</code></pre>
<p>With Python people usually don't go out of their way to prevent you from making mistakes the way they do in (e.g.) C++, and so all sorts of things are possible. But with this power comes responsibility, namely the responsibility not to shoot yourself in the foot when there's no need to.</p>
http://ask.sagemath.org/question/7783/how-to-set-mutable-an-inmutable-sequence/?comment=22322#post-id-22322@Tik: you can search as well as I can! I didn't find anything, but I didn't look that hard. @kcrisman: _is_immutable isn't a Python flag, it's on the Sage side.Thu, 06 Jan 2011 15:57:36 -0600http://ask.sagemath.org/question/7783/how-to-set-mutable-an-inmutable-sequence/?comment=22322#post-id-22322Answer by kcrisman for <p>I think the title is quite descriptive.</p>
<p>I would like to know, how can you set mutable an inmutable sequence. Is that even possible?</p>
<p>I would be very grateful if you can link me the file of the sage documentation where I can find that info.</p>
<p>Thanks.</p>
http://ask.sagemath.org/question/7783/how-to-set-mutable-an-inmutable-sequence/?answer=11917#post-id-11917Well, you can do the following, but I'm not sure if this is what you want:
sage: seq = (1,2,3,4)
sage: seq[0]=2
---------------------------------------------------------------------------
TypeError: 'tuple' object does not support item assignment
sage: seq2 = list(seq)
sage: seq2[0]=2
sage: seq2
[2, 2, 3, 4]
However, my guess is that you want to make `seq` above a mutable object. I don't think this is possible - you can try to search the [Python site](http://www.python.org) to see if you can find an exception to this. In general, if you want to make something mutable, the type has to start as mutable, as far as I know, and Python is strongly typed (i.e. you can't change the type of an object once it's created). Mon, 03 Jan 2011 04:05:02 -0600http://ask.sagemath.org/question/7783/how-to-set-mutable-an-inmutable-sequence/?answer=11917#post-id-11917Comment by kcrisman for <p>Well, you can do the following, but I'm not sure if this is what you want:</p>
<pre><code>sage: seq = (1,2,3,4)
sage: seq[0]=2
---------------------------------------------------------------------------
TypeError: 'tuple' object does not support item assignment
sage: seq2 = list(seq)
sage: seq2[0]=2
sage: seq2
[2, 2, 3, 4]
</code></pre>
<p>However, my guess is that you want to make <code>seq</code> above a mutable object. I don't think this is possible - you can try to search the <a href="http://www.python.org">Python site</a> to see if you can find an exception to this. In general, if you want to make something mutable, the type has to start as mutable, as far as I know, and Python is strongly typed (i.e. you can't change the type of an object once it's created). </p>
http://ask.sagemath.org/question/7783/how-to-set-mutable-an-inmutable-sequence/?comment=22347#post-id-22347And no, I'm not interested in an argument with someone in the peanut gallery about 'duck' or 'strong' typing or anything else about what Python's typing should be called...Mon, 03 Jan 2011 04:05:37 -0600http://ask.sagemath.org/question/7783/how-to-set-mutable-an-inmutable-sequence/?comment=22347#post-id-22347Answer by Tlk for <p>I think the title is quite descriptive.</p>
<p>I would like to know, how can you set mutable an inmutable sequence. Is that even possible?</p>
<p>I would be very grateful if you can link me the file of the sage documentation where I can find that info.</p>
<p>Thanks.</p>
http://ask.sagemath.org/question/7783/how-to-set-mutable-an-inmutable-sequence/?answer=11927#post-id-11927I think i have found the solution:
v._set_ inmutable=FALSE
Anything can link me something about that on the Documentation?Wed, 05 Jan 2011 10:03:50 -0600http://ask.sagemath.org/question/7783/how-to-set-mutable-an-inmutable-sequence/?answer=11927#post-id-11927