Ask Your Question
0

Recovering an overwritten Sage built-in name

asked 2015-06-20 01:19:49 +0100

candide gravatar image

In Python 2.7, if you accidentally overwrite a builtin name (eg max, str, int, ...), you can recover it by calling the __builtins__ module :

>>> max(2020, 42)
2020
>>> max=2038
>>> max(2020, 42)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'int' object is not callable
>>> max=__builtins__.max
>>> max(2020, 42)
2020
>>>

What is the equivalent for Sage built-in names (there are about 2000 so accident can occur) ? For instance, imagine I overwrite the complex number I :

sage: I=42

How can I recover it ?

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
1

answered 2015-06-20 09:50:23 +0100

candide gravatar image

This previous response gives a clue : the reset function. Passing the variables as a string argument, the reset function allows to selectively restore some Sage global variables in their initial state. Example :

sage: max(2000,2038)
2038
sage: max=42
sage: max(2000,2038)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-3-1c36721a9cff> in <module>()
----> 1 max(Integer(2000),Integer(2038))

TypeError: 'sage.rings.integer.Integer' object is not callable
sage: I=42
sage: reset("max I")
sage: I*I
-1
sage: max(2000,2038)
2038
edit flag offensive delete link more
0

answered 2015-06-20 08:22:18 +0100

rws gravatar image

The answer depends. Constants have the path sage.symbolic.constants. Global functions like max are available under sage.all---however max is really max_symbolic, it only prints as max. In doubt, the source is your friend.

sage: sage.all.max_symbolic
max
sage: max=1234
sage: max=sage.all.max_symbolic
sage: max(7,8)
8

sage: I=1
sage: I=sage.symbolic.constants.I
sage: I^2
-1
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: 2015-06-20 01:19:49 +0100

Seen: 530 times

Last updated: Jun 20 '15