# 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.

edit retag close merge delete

Sort by ยป oldest newest most voted

The 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

more

Thanks. 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?

( 2011-06-20 19:57:55 +0200 )edit

See 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.

( 2011-06-21 10:01:21 +0200 )edit

Another 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

more

How can I clear all variables? Is there any command like ClearAll?

( 2021-04-03 19:23:19 +0200 )edit