Indeed I did! Although he could have gotten the effect of
> v = vars(__main__)
v = __main__.__dict__
There are a lot of ways to do this!
> Who needs eval when you have vars?
Just because of this:
> | ...
> | A more general trick is to use 'eval' to map a variable's name (a string)
> | to the variable's current binding. E.g., ...
eval looks through the bindings of local, global, and builtin names,
while vars() only finds the local ones. In the interactive examples this
gets confused, because the local & global namespaces are the same when
typing stuff at Python at the top level. So I suspect eval has less
"gotcha" potential for beginners. E.g.,
>>> a = 12
>>> print eval('a')
>>> print vars()['a']
>>> def f():
... print eval('a')
... print vars()['a'] # blows up
Traceback (innermost last):
File "<stdin>", line 1
File "<stdin>", line 3
Understanding why eval "works" in both contexts but vars() doesn't isn't
difficult, but probably isn't _obvious_ to a beginner either.
whatever!-ly y'rs - tim
Tim Peters firstname.lastname@example.org
not speaking for Kendall Square Research Corp