newbie wants to eval()

lynx noone at nowhere.net
Sat Jun 30 19:44:17 EDT 2001


"Alex Martelli" <aleaxit at yahoo.com>, in <9hlhkl02cu9 at enews1.newsguy.com>:
> "lynx" <noone at nowhere.net> wrote in message
> news:3b3e3e81$0$88189$2c3e98f8 at news.voyager.net...

[...]
> You'd need to use the 'exec' statement (shudder), but fortunately you
> can easily do better than that, since...:

hmm. is there something inherently evil about exec that i should know
about...?

> Global variables are really attributes of an object, the module object
> to be precise.  Setting any object's attributes is most easily done via
> built-in function setattr().

> So you need a reference to the object that's the module you want to
> affect.  Assuming this is the module containing the function you're
> writing, simplest may be:
>     import sys
>     mymod = sys.modules[__name__]
> and now mymod is the reference you require.

looks interesting. should that snippet be run in the function where i'm
trying to change these globals, or in the main program and that reference
passed in as an argument to the function?

honestly, myself, i'd be more upset and shuddering at this kind of
manual rooting around in Python's own internal workings than at the
exec() statement, which seems designed to hide this sort of magic from
me; unless, of course, there's something strange about exec that i don't
know of...?

-- 
   PGP/GnuPG key (ID 1024D/BFE0D6D0) available from keyservers everywhere
    Key fingerprint = 3EBC 97FC 68AA 65F1 65E6  3D36 35F6 4213 BFE0 D6D0
                             "...life goes on
                  long after the thrill of living is gone..."



More information about the Python-list mailing list