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.Tue, 21 Jun 2011 07:30:16 -0500How can I clear an assignment to a variable?http://ask.sagemath.org/question/8172/how-can-i-clear-an-assignment-to-a-variable/If I assign a symbolic expression to a variable, how do I clear the assignment as if I didn't assign anything to it in the first place?
For example,
sage: var('a,b')
(a, b)
sage: x=a+b
sage: x
a + b
sage: var(x)
a + b
sage: x
a + b
sage: x=x
sage: x
a + b
Here, I assigned a+b to x. But I can't 'unassign' it.
In Mathematica this can be done with Clear[x].
Thanks.Mon, 20 Jun 2011 09:46:38 -0500http://ask.sagemath.org/question/8172/how-can-i-clear-an-assignment-to-a-variable/Answer by Volker Braun for <p>If I assign a symbolic expression to a variable, how do I clear the assignment as if I didn't assign anything to it in the first place?</p>
<p>For example,</p>
<pre><code>sage: var('a,b')
(a, b)
sage: x=a+b
sage: x
a + b
sage: var(x)
a + b
sage: x
a + b
sage: x=x
sage: x
a + b
</code></pre>
<p>Here, I assigned a+b to x. But I can't 'unassign' it.</p>
<p>In Mathematica this can be done with Clear[x].</p>
<p>Thanks.</p>
http://ask.sagemath.org/question/8172/how-can-i-clear-an-assignment-to-a-variable/?answer=12451#post-id-12451Another possibility is to use Python's **del** to delete an object:
sage: del x
sage: x
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
/home/vbraun/opt/sage-4.7.1.alpha2/local/include/singular/<ipython console> in <module>()
NameError: name 'x' is not defined
sage: x = var('x')
sage: x
x
Tue, 21 Jun 2011 07:30:16 -0500http://ask.sagemath.org/question/8172/how-can-i-clear-an-assignment-to-a-variable/?answer=12451#post-id-12451Answer by kcrisman for <p>If I assign a symbolic expression to a variable, how do I clear the assignment as if I didn't assign anything to it in the first place?</p>
<p>For example,</p>
<pre><code>sage: var('a,b')
(a, b)
sage: x=a+b
sage: x
a + b
sage: var(x)
a + b
sage: x
a + b
sage: x=x
sage: x
a + b
</code></pre>
<p>Here, I assigned a+b to x. But I can't 'unassign' it.</p>
<p>In Mathematica this can be done with Clear[x].</p>
<p>Thanks.</p>
http://ask.sagemath.org/question/8172/how-can-i-clear-an-assignment-to-a-variable/?answer=12447#post-id-12447The reset command may do what you want.
sage: var('a,b')
(a, b)
sage: x = a+b
sage: x
a + b
sage: reset('x')
sage: x
x
Mon, 20 Jun 2011 10:01:12 -0500http://ask.sagemath.org/question/8172/how-can-i-clear-an-assignment-to-a-variable/?answer=12447#post-id-12447Comment by omoplata for <p>The reset command may do what you want.</p>
<pre><code>sage: var('a,b')
(a, b)
sage: x = a+b
sage: x
a + b
sage: reset('x')
sage: x
x
</code></pre>
http://ask.sagemath.org/question/8172/how-can-i-clear-an-assignment-to-a-variable/?comment=21569#post-id-21569Thanks. I've made a mistake in the code I posted. I've typed var(x) instead of var('x'). If var('x') is used, that removes the assignment too. I guess sage makes a completely new variable named x?Mon, 20 Jun 2011 12:57:55 -0500http://ask.sagemath.org/question/8172/how-can-i-clear-an-assignment-to-a-variable/?comment=21569#post-id-21569Comment by kcrisman for <p>The reset command may do what you want.</p>
<pre><code>sage: var('a,b')
(a, b)
sage: x = a+b
sage: x
a + b
sage: reset('x')
sage: x
x
</code></pre>
http://ask.sagemath.org/question/8172/how-can-i-clear-an-assignment-to-a-variable/?comment=21567#post-id-21567See the answer to your previous question at http://ask.sagemath.org/question/597/difference-between-varx-and-xvarx - I think that is the best explanation.Tue, 21 Jun 2011 03:01:21 -0500http://ask.sagemath.org/question/8172/how-can-i-clear-an-assignment-to-a-variable/?comment=21567#post-id-21567