Ask Your Question
1

How to display all the symbolic variables and functions?

asked 2018-04-23 00:06:12 +0100

ionsme gravatar image

I have already declared a lot of stuff. How do I see what is already declared. something like: show (variables()) and show(functions()).

Something analogous to assumptions(). And why doesn't vars() work?

edit retag flag offensive close merge delete

2 Answers

Sort by » oldest newest most voted
3

answered 2018-04-24 13:38:47 +0100

Sébastien gravatar image

updated 2018-06-05 22:22:06 +0100

You may use show_identifiers:

sage: show_identifiers()
['In', 'Out', 'exit', 'get_ipython', 'quit']
sage: a = 2
sage: def function_square(b):
....:     return b^2
....: 
sage: show_identifiers()
['In', 'Out', 'a', 'exit', 'function_square', 'get_ipython', 'quit']

Then using the locals() which returns a dictionary containing the current scope's local variables, you can recover the objects:

sage: D = locals()
sage: D['a']
2
sage: D['function_square']
<function function_square at 0x7fa7ddd35f50>
sage: D['quit']
<IPython.core.autocall.ExitAutocall object at 0x7fa94bc4fa90>

These tools are used by the save_session function in Sage.

EDIT June 5th 2018: Another option is to use magic IPython functions %who and %whos :

sage: %who
a    function_square     
sage: %whos
Variable          Type        Data/Info
---------------------------------------
a                 Integer     2
function_square   function    <function function_square at 0x7f28cfb2ac08>
edit flag offensive delete link more
2

answered 2018-04-23 07:40:41 +0100

nbruin gravatar image

You can use

[k for k,v in list(globals().iteritems()) if isinstance(v,sage.symbolic.expression.Expression)]

to display all the names in the global namespace to which a symbolic expression is bound and

[k for k,v in list(globals().iteritems()) if isinstance(v,sage.symbolic.function.Function)]

to find all the names to which a symbolic function is bound. You can vary the query as desired.

You can of course look at globals().keys() directly, but you'll probably find that's a bit unwieldy (and it is what vars() ends up doing too (and as far as I can see it works as documented).

While it may seem at first that inspecting globals() gives you a lot of information about the system, it's in practice rarely useful. Whether something is bound to a global name ends up being not so important in Python.

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: 2018-04-23 00:06:12 +0100

Seen: 1,486 times

Last updated: Jun 05 '18