OT - how to create variables from a cvs file?

Christian wrote:

jmf wrote:

locals() beeing the dict containing the vars...

li = ['field1', 'field2', 'field3']
for e in li:

       locals()[e] = None

print field1

None

print field2

None

print field3

None

I read somewhere that both globals() and locals() are not meant for writing. So
this might or might not work always.

AFAIK, the dictionary returned by globals() is used to look up global variables, and adding an entry will add a variable.

However, the dictionary returned by locals() is usually a made-up dictionary which gives access to local variables, but isn't consulted by the Python interpreter. Inside functions, local variables are accessed through a pre-compiled array of references (the decompiled code for a small function will show this) so changing a locals() dictionary changes nothing.

Outside any function, a call to locals() returns a reference to the same dictionary returned by globals(), so the example above works.
Consider
Python 2.4.2 (#1, Jan 23 2006, 21:24:54)
[GCC 3.3.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.

def f():

... d = locals()
... d['a'] = 1
... d['b'] = 2
... print a, b
...

f()

Traceback (most recent call last):
   File "<stdin>", line 1, in ?
   File "<stdin>", line 5, in f
NameError: global name 'a' is not defined

                                          
  Mel.