Avoiding `exec', how to?

Aahz aahz at pythoncraft.com
Thu May 30 17:09:48 EDT 2002


In article <mailman.1022786751.23084.python-list at python.org>,
=?iso-8859-1?q?Fran=E7ois?= Pinard <pinard at iro.umontreal.ca> wrote:
>
>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.

import m
for name, value in defs.__dict__.items():
    if not name.startswith('_'):
        setattr(m, name, value)

m is of course the name of the module.
-- 
Aahz (aahz at pythoncraft.com)           <*>         http://www.pythoncraft.com/

"In the end, outside of spy agencies, people are far too trusting and
willing to help."  --Ira Winkler



More information about the Python-list mailing list