ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sat, 03 Apr 2021 19:23:19 +0200How can I clear an assignment to a variable?https://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 16:46:38 +0200https://ask.sagemath.org/question/8172/how-can-i-clear-an-assignment-to-a-variable/Answer 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>
https://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 17:01:12 +0200https://ask.sagemath.org/question/8172/how-can-i-clear-an-assignment-to-a-variable/?answer=12447#post-id-12447Comment 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>
https://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 10:01:21 +0200https://ask.sagemath.org/question/8172/how-can-i-clear-an-assignment-to-a-variable/?comment=21567#post-id-21567Comment 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>
https://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 19:57:55 +0200https://ask.sagemath.org/question/8172/how-can-i-clear-an-assignment-to-a-variable/?comment=21569#post-id-21569Answer 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>
https://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 14:30:16 +0200https://ask.sagemath.org/question/8172/how-can-i-clear-an-assignment-to-a-variable/?answer=12451#post-id-12451Comment by Apoorv for <p>Another possibility is to use Python's <strong>del</strong> to delete an object:</p>
<pre><code>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
</code></pre>
https://ask.sagemath.org/question/8172/how-can-i-clear-an-assignment-to-a-variable/?comment=56482#post-id-56482How can I clear all variables? Is there any command like ClearAll?Sat, 03 Apr 2021 19:23:19 +0200https://ask.sagemath.org/question/8172/how-can-i-clear-an-assignment-to-a-variable/?comment=56482#post-id-56482