Ask Your Question

How to display all the symbolic variables and functions?

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

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-24 13:38:47 +0200

Sébastien gravatar image

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

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

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

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


Asked: 2018-04-23 00:06:12 +0200

Seen: 1,314 times

Last updated: Jun 05 '18