Evaluation of variable as f-string
Chris Angelico
rosuav at gmail.com
Tue Feb 7 13:52:14 EST 2023
On Wed, 8 Feb 2023 at 05:30, Stefan Ram <ram at zedat.fu-berlin.de> wrote:
>
> Rob Cliffe <rob.cliffe at 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
More information about the Python-list
mailing list