Noobie python shell question

Chris Rebert clp2 at rebertia.com
Sun Nov 29 23:14:13 EST 2009


On Sun, Nov 29, 2009 at 7:53 PM, Tim Chase
<python.list at tim.thechases.com> wrote:
<snip>
> (as an aside, is there a way to get a local/global variable from a string
> like one can fetch a variable from a class/object with getattr()?  Something
> like getattr(magic_namespace_here, "hello") used in the above context?  I
> know it can be done with eval(), but that's generally considered unsafe
> unless you vet your input thoroughly)

locals()["hello"]  and  globals()["hello"], respectively

Cheers,
Chris
--
http://blog.rebertia.com



More information about the Python-list mailing list