ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Tue, 24 Apr 2018 06:38:47 -0500How to display all the symbolic variables and functions?http://ask.sagemath.org/question/42126/how-to-display-all-the-symbolic-variables-and-functions/ 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?Sun, 22 Apr 2018 17:06:12 -0500http://ask.sagemath.org/question/42126/how-to-display-all-the-symbolic-variables-and-functions/Answer by Sébastien for <p>I have already declared a lot of stuff. How do I see what is already declared. something like: show (variables()) and show(functions()).</p>
<p>Something analogous to assumptions(). And why doesn't vars() work?</p>
http://ask.sagemath.org/question/42126/how-to-display-all-the-symbolic-variables-and-functions/?answer=42146#post-id-42146You 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](https://ipython.org/ipython-doc/3/interactive/reference.html#dynamic-object-information) `%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>Tue, 24 Apr 2018 06:38:47 -0500http://ask.sagemath.org/question/42126/how-to-display-all-the-symbolic-variables-and-functions/?answer=42146#post-id-42146Answer by nbruin for <p>I have already declared a lot of stuff. How do I see what is already declared. something like: show (variables()) and show(functions()).</p>
<p>Something analogous to assumptions(). And why doesn't vars() work?</p>
http://ask.sagemath.org/question/42126/how-to-display-all-the-symbolic-variables-and-functions/?answer=42131#post-id-42131You 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.Mon, 23 Apr 2018 00:40:41 -0500http://ask.sagemath.org/question/42126/how-to-display-all-the-symbolic-variables-and-functions/?answer=42131#post-id-42131