Hi Stephen,
If you want to list all sympy symbols that you have created in a Python script, one straightforward way is to use Python’s built-in globals()
to scan the current namespace and check for instances of sympy.Symbol
.
Here's a simple example to illustrate:
>>> from sympy import Symbol >>> from sympy.abc import a, b, c >>> x = Symbol('x') >>> y = Symbol('y') >>> z = 3 # this is not a SymPy symbol >>> symbols_used = [name for name, obj in globals().items() if isinstance(obj, Symbol)] >>> print("Symbols used :", symbols_used)
This would output:
Symbols used : ['a', 'b', 'c', 'x', 'y']
But do let me know if you are looking for something more specific. Happy to help further!
Best regards,
Krishnav Bajoria.
How do I list ALL sympy symbols created in a Python script?
--
You received this message because you are subscribed to the Google Groups "sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/sympy/2f682bea-f462-4188-9873-82c9ebafe889n%40googlegroups.com.
On 28 Jun 2025, at 07:26, Sangyub Lee <syle...@gmail.com> wrote:
I don't think we should add such functionality to the library. I would not use locals() or globals() introspection in library because it lead to issues with garbage collection, introduce security vulnerabilities, and create thread safety concerns.
To view this discussion visit https://groups.google.com/d/msgid/sympy/f0234176-abe1-4451-a6bd-9c58bb35bb06n%40googlegroups.com.
I have been following this discussion as I use a library of sympy (sympy.physics.mechanics) quite a bit.
Would Krishnav’s one line code not do what Stephen wants, or am I missing something?
Thanks!
To view this discussion visit https://groups.google.com/d/msgid/sympy/9f2e3377-26ce-4894-8f14-2b15c4869812n%40googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/sympy/0c0901dbe80d%2414fff820%243effe860%24%40gmail.com.
To view this discussion visit https://groups.google.com/d/msgid/sympy/0c0901dbe80d%2414fff820%243effe860%24%40gmail.com.
Honestly, that's absurd to have a one-liner working function in a library. If a function can be written in one line, it gives twice as strong a reason that it should not belong to the library.
And furthermore, I don't agree with having list_symbols because I don't even want to encourage people to program in Python in such a fragile manner. Why should we help someone who forgets where they defined their variables?
So why don’t you define you own function:
def list_symbols():
return Krishnaw’s list
Would that not work?
To view this discussion visit https://groups.google.com/d/msgid/sympy/126633FB-0907-496F-BE65-EADA8192C5FF%40gmail.com.
In [1]: from sympy import Symbol
In [2]: def generate_symbols():
...: for i in range(10):
...: globals()[f"s{i}"] = Symbol(f"s{i}")
...:
In [3]: def list_symbols():
...: return [name for name, obj in globals().items() if isinstance(obj, Symbol)] # Krishnav's one liner
...:
In [4]: import threading
In [5]: thread1 = threading.Thread(target=generate_symbols)
...: thread2 = threading.Thread(target=list_symbols)
In [6]: thread1.start()
...: thread2.start()
def list_symbols():
return [name for name, obj in globals().items() if isinstance(obj, Symbol)] # Krishnav's one liner
To view this discussion visit https://groups.google.com/d/msgid/sympy/0c3801dbe813%2450a8bd20%24f1fa3760%24%40gmail.com.
Dear Aasim,
thanks!
Just learned something new.
Peter
To view this discussion visit https://groups.google.com/d/msgid/sympy/CAO0%2BxCUyzFy-x1i5P2_u%2BjA%3DBLHnH35LE98p3kDCQBwYKSkt4A%40mail.gmail.com.
I think you sent me a message to my personal email rather than the mailing list, but I would like to give you some friendly advice.
I think you should reconsider your position when you see me or someone else disagreeing with your points. I have worked on SymPy longer, and if I wanted to push every complaint I have, it would be 100 times longer, but I don't do it for good reason. The expectation that I should agree with your personal complaints doesn't sound fair in this case.
I don't think that 'list_symbols' is useful for everyone. At least I haven't found such an inconvenience personally, and I haven't needed it. That's not because I have better memory or such, but because I already know how to navigate such situations.
The problem that things get confusing after global variables are overwritten does not only exist with respect to SymPy, but for all Python variables. And that is especially problematic if the definition is recursive, such as:
x = x + 1
or
api_key = requests.get(... + api_key)
because running the same script can break things
If we implement something that is too specific to SymPy, like SymPy symbols, I would also find that too restrictive to be useful anyway.
The reason that I (and some others) don't complain about such issues is that we understand these are user workflow issues, not library issues.
I generally suggest that:
Anyway, the point is that if you are wise, you would realize it's definitely better to improve your own skills and terminal usage practices than attempting to fix the library.
--
You received this message because you are subscribed to a topic in the Google Groups "sympy" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/sympy/Z8XPIGE8J0Q/unsubscribe.
To unsubscribe from this group and all its topics, send an email to sympy+un...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/sympy/602ca057-1d3c-4a24-bf85-2ea45645a2f2n%40googlegroups.com.