Ask Your Question
3

How can I clear an assignment to a variable?

asked 2011-06-20 16:46:38 +0200

omoplata gravatar image

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 flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
6

answered 2011-06-20 17:01:12 +0200

kcrisman gravatar image

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
edit flag offensive delete link more

Comments

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?

omoplata gravatar imageomoplata ( 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.

kcrisman gravatar imagekcrisman ( 2011-06-21 10:01:21 +0200 )edit
3

answered 2011-06-21 14:30:16 +0200

Volker Braun gravatar image

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
edit flag offensive delete link more

Comments

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

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

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2011-06-20 16:46:38 +0200

Seen: 14,493 times

Last updated: Jun 21 '11