Ask Your Question

How to display all the symbolic variables and functions?

asked 2018-04-22 17:06:12 -0500

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

answered 2018-04-23 00:40:41 -0500

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

answered 2018-04-24 06:38:47 -0500

Sébastien gravatar image

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']
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 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


Asked: 2018-04-22 17:06:12 -0500

Seen: 70 times

Last updated: Apr 24