Avoiding `exec', how to? [and why usie it in the first place?]

Steve Holden sholden at holdenweb.com
Fri May 31 05:29:11 EDT 2002


"François Pinard" <pinard at iro.umontreal.ca> wrote ...
> Hi, my fellow snakes :-).
>
> I do not like using `exec', and wonder if there is a way to avoid it in:
>
>
> # Fabriquer une variable globale pour chaque élément de DEFS.
> for name, value in defs.__dict__.items():
>     if name[0] != '_':
>         # J'aimerais bien éviter `exec'...
>         exec '%s = %s' % (name, repr(value))
> del defs, name, value
>
>
> The goal here is to initialise one global variable per item in DEFS,
> preserving the item name and value in each.
>

I was hoping we might see some discussion of *why* you want to do this.
Surely unless you are similarly using getattr()-based code to retrieve these
globals you are actually writing *code* that uses those global names. In
which case, why set them from another dictionary?

If you *are* using getattr() to access the "unpredictable" global names, why
not simply use the dictionary in the first place?

puzzled-ly y'rs  - steve
--
-----------------------------------------------------------------------
Steve Holden                                 http://www.holdenweb.com/
Python Web Programming                http://pydish.holdenweb.com/pwp/
-----------------------------------------------------------------------








More information about the Python-list mailing list