Ask Your Question
0

Desattribution of a value to a variable

asked 2020-05-12 16:53:36 +0100

Cyrille gravatar image

Suppose I have attributed to a the value 3. But for the following operation I want to use anew a as a variable how should I operate, redefine a ?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2020-05-12 18:26:32 +0100

tmonteil gravatar image

updated 2020-05-12 18:28:20 +0100

A Python name is just a pointer to a Python/Sage object:

sage: a = 3
sage: a+a
6

Now, if you redefine it, it will just point to the new object:

sage: a = 12
sage: a
12
sage: a^2
144

If you want the symbol a (not to be confused with the Python name a), you can do:

sage: SR.var('a')
a

Any Python name can point to such symbol:

sage: b = SR.var('a')
sage: b
a
sage: (b+1)^2
(a + 1)^2

What confuses most people is that the shortcut:

sage: var('a')
a

both returns the symbol a and let the Python name a point to this symbol, in particular, it removes the previous pointer of the Python name a :

sage: a        # this a is a Python name
a              # this a is a symbol, pointed bythe previous Python name
edit flag offensive delete link more

Your Answer

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

Add Answer

Question Tools

1 follower

Stats

Asked: 2020-05-12 16:53:36 +0100

Seen: 187 times

Last updated: May 12 '20