Dear Aasim,
thanks! Just learned something new. Peter From: sympy@googlegroups.com <sympy@googlegroups.com> On Behalf Of Aasim Sent: Saturday, June 28, 2025 12:12 PM To: sympy@googlegroups.com Subject: Re: [sympy] Listing all symbols in sympy I agree with Sangyub here, it is problematic when we work with globals() and locals() (have suffered before). And to Peter, yes, Krishnav's one liner code works, at least it lists all the Symbol objects created globally, not locally though(like inside a function or inside a class defined in the terminal application) . We don't really need such typical methods for a very trivial use case in the library(any contributor can stumble upon a maintainer mentioning this exact same thing very often). Using globals() and locals(), again, is concerning. The concern that stands out the most for me here is thread safety. For example, let us assume that for once we indeed start working on adding the list_symbols() method that Stephen mentioned, then I will use Krishnav's one liner, something like: 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() thread1 might still be mutating the global namespace while thread2 tries accessing it, which is unsafe (a very simple example of a race condition here). Similarly, when you use globals() or locals(), you're creating references to objects that might otherwise be eligible for garbage collection. Because the globals() dictionary often keeps references alive for longer than we might intend it to. Another example could be, in pretty simple web applications the dev might add a secret API key to the application's code before hosting it and if in the python backend they are using SymPy which in turn uses something like list_symbols() proposed here, can introduce a vulnerability that publicly exposes the secret API key (hypothesizing here but very much possible). I would suggest the same thing as Peter in conclusion: Stephen, you can just create a simple startup script for yourself that defines such trivial functionality(ies). I use IPython myself and I have a startup script written for it, if I had a similar use case as yours I would just add something like this to my startup_ipython.py: def list_symbols(): return [name for name, obj in globals().items() if isinstance(obj, Symbol)] # Krishnav's one liner and then use the shell by calling `ipython -i startup_ipython.py`. On Sat, 28 Jun 2025 at 15:29, <peter.stahlec...@gmail.com <mailto:peter.stahlec...@gmail.com> > wrote: So why don’t you define you own function: def list_symbols(): return Krishnaw’s list Would that not work? From: sympy@googlegroups.com <mailto:sympy@googlegroups.com> <sympy@googlegroups.com <mailto:sympy@googlegroups.com> > On Behalf Of stephen.j.learmo...@gmail.com <mailto:stephen.j.learmo...@gmail.com> Sent: Saturday, June 28, 2025 11:31 AM To: sympy@googlegroups.com <mailto:sympy@googlegroups.com> Subject: Re: [sympy] Listing all symbols in sympy Yes, that single line of code works but it’s simpler and faster to be able to just type a short command like “list_symbols()” to list all the symbols created in terminal. Sent from my iPhone. On 28 Jun 2025, at 10:14, peter.stahlec...@gmail.com <mailto:peter.stahlec...@gmail.com> wrote: 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! From: sympy@googlegroups.com <mailto:sympy@googlegroups.com> <sympy@googlegroups.com <mailto:sympy@googlegroups.com> > On Behalf Of Sangyub Lee Sent: Saturday, June 28, 2025 10:59 AM To: sympy <sympy@googlegroups.com <mailto:sympy@googlegroups.com> > Subject: Re: [sympy] Listing all symbols in sympy I don't understand `locals()` or `globals()` very well, but it's always good that we don't implement something directly based on functionality that we don't feel safe using. Or will you put in the effort to address those concerns anyway Regardless, it is primarily your responsibility to prove a strong reason why we need such functionality in SymPy. We don't have a responsibility to implement something general to solve the trivial terminal problems that you face. On Saturday, June 28, 2025 at 10:41:48 AM UTC+2 stephen.j...@gmail.com <mailto:stephen.j...@gmail.com> wrote: What does “…locals() or globals() introspection…” mean? Please prove to me that “… it lead to issues with garbage collection, introduce security vulnerabilities and create thread safety concerns”. I won’t be moving beyond Python. What do you mean by “…domain-specific…”! Sent from my iPhone. On 28 Jun 2025, at 07:26, Sangyub Lee <syle...@gmail.com <mailto: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. I think you would need to move beyond Python and use a different domain-specific language or user interface to implement such functionality in a secure way. I suggest consulting with experienced software engineers or IT agencies to implement that approach. On Wednesday, June 25, 2025 at 8:17:49 PM UTC+2 stephen.j...@gmail.com <mailto:stephen.j...@gmail.com> wrote: My “use case” is that suppose you are executing Python code in the Mac terminal application to test out some code and you lose track of what variables you have created and you want to delete some or all of them to start over. A function which lists all the created variables would be useful so you can delete them all and start over. A very basic function. Sent from my iPhone. > On 25 Jun 2025, at 17:46, Aaron Meurer <asme...@gmail.com > <mailto:asme...@gmail.com> > wrote: > > What is your use-case for such a function? In most cases if you are > writing a script you know what symbols are defined because you have > defined them all statically somewhere, so there's no need to > programmatically get a list of them. > > Aaron Meurer > >> On Mon, Jun 23, 2025 at 9:14 AM Stephen Learmonth >> <stephen.j...@gmail.com <mailto:stephen.j...@gmail.com> > wrote: >> >> Thanks Krishnav, >> >> It would have been really useful if Sympy just had a function like >> list_symbols() oir something from the start. >> >> It''s such a basic thing to want to be able to do! >> >> Stephen >> >>> On Monday, 23 June 2025 at 16:00:38 UTC+1 bajoria...@gmail.com >>> <mailto:bajoria...@gmail.com> wrote: >>> >>> 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. >>> >>> >>> On Mon, Jun 23, 2025 at 8:07 PM Stephen Learmonth <stephen.j...@gmail.com >>> <mailto:stephen.j...@gmail.com> > wrote: >>>> >>>> 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 >>>> <mailto:sympy+un...@googlegroups.com> . >>>> To view this discussion visit >>>> https://groups.google.com/d/msgid/sympy/2f682bea-f462-4188-9873-82c9ebafe889n%40googlegroups.com. >>>> >> >> -- >> 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 <mailto:sympy+un...@googlegroups.com> >> . >> To view this discussion visit >> https://groups.google.com/d/msgid/sympy/c41df7a9-b27c-4e81-9fcd-85a52789b4e6n%40googlegroups.com. >> > > -- > 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 <mailto:sympy+un...@googlegroups.com> . > To view this discussion visit > https://groups.google.com/d/msgid/sympy/CAKgW%3D6%2B%2B_feNU%3D1psGQ1H7PapFHO67DtaqHKW8PiR59iZmrRDA%40mail.gmail.com. > -- 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 <mailto:sympy+un...@googlegroups.com> . To view this discussion visit https://groups.google.com/d/msgid/sympy/f0234176-abe1-4451-a6bd-9c58bb35bb06n%40googlegroups.com <https://groups.google.com/d/msgid/sympy/f0234176-abe1-4451-a6bd-9c58bb35bb06n%40googlegroups.com?utm_medium=email&utm_source=footer> . -- 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+unsubscr...@googlegroups.com <mailto:sympy+unsubscr...@googlegroups.com> . To view this discussion visit https://groups.google.com/d/msgid/sympy/9f2e3377-26ce-4894-8f14-2b15c4869812n%40googlegroups.com <https://groups.google.com/d/msgid/sympy/9f2e3377-26ce-4894-8f14-2b15c4869812n%40googlegroups.com?utm_medium=email&utm_source=footer> . -- 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+unsubscr...@googlegroups.com <mailto:sympy+unsubscr...@googlegroups.com> . To view this discussion visit https://groups.google.com/d/msgid/sympy/0c0901dbe80d%2414fff820%243effe860%24%40gmail.com <https://groups.google.com/d/msgid/sympy/0c0901dbe80d%2414fff820%243effe860%24%40gmail.com?utm_medium=email&utm_source=footer> . -- 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+unsubscr...@googlegroups.com <mailto:sympy+unsubscr...@googlegroups.com> . To view this discussion visit https://groups.google.com/d/msgid/sympy/126633FB-0907-496F-BE65-EADA8192C5FF%40gmail.com <https://groups.google.com/d/msgid/sympy/126633FB-0907-496F-BE65-EADA8192C5FF%40gmail.com?utm_medium=email&utm_source=footer> . -- 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+unsubscr...@googlegroups.com <mailto:sympy+unsubscr...@googlegroups.com> . To view this discussion visit https://groups.google.com/d/msgid/sympy/0c3801dbe813%2450a8bd20%24f1fa3760%24%40gmail.com <https://groups.google.com/d/msgid/sympy/0c3801dbe813%2450a8bd20%24f1fa3760%24%40gmail.com?utm_medium=email&utm_source=footer> . -- 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+unsubscr...@googlegroups.com <mailto:sympy+unsubscr...@googlegroups.com> . To view this discussion visit https://groups.google.com/d/msgid/sympy/CAO0%2BxCUyzFy-x1i5P2_u%2BjA%3DBLHnH35LE98p3kDCQBwYKSkt4A%40mail.gmail.com <https://groups.google.com/d/msgid/sympy/CAO0%2BxCUyzFy-x1i5P2_u%2BjA%3DBLHnH35LE98p3kDCQBwYKSkt4A%40mail.gmail.com?utm_medium=email&utm_source=footer> . -- 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+unsubscr...@googlegroups.com. To view this discussion visit https://groups.google.com/d/msgid/sympy/0c8701dbe823%24c81413e0%24583c3ba0%24%40gmail.com.