On Wed, 8 Feb 2023 at 05:30, Stefan Ram <r...@zedat.fu-berlin.de> wrote: > > Rob Cliffe <rob.cli...@btinternet.com> writes: > >Does that mean that it is not possible to have a (built-in) function > >that would construct and return a dictionary of all available variables > >and their values? If it were possible, it could be useful, and there > >would be no impact on Python run-time speed if it were only constructed > >on demand. > > Here's a quick attempt to get local and non-local names as a set. > It might only work under some implementations of Python. > > main.py > > set_class = set > > def f(): > x = 0 > def g(): > set = set_class() > for n in g.__qualname__.split( '.' ): > if n != '<locals>': > set = set.union( set_class( eval( n ).__code__.co_varnames )) > print( set ) > g() > f() > > output > > {'n', 'set', 'x'} >
Breaks on wrapped functions. Also, how are you going to get the values of those variables? ChrisA -- https://mail.python.org/mailman/listinfo/python-list