creating lists question?

Josiah Carlson jcarlson at
Sat Mar 27 07:56:30 CET 2004

>>for name in list1:
>>    locals()[name] = []
> Modifying locals() is not entirely like creating a variable:

It all depends on your scope:
 >>> locals()['b'] = 'y'
 >>> b

But why does this work?
 >>> locals() is globals()

Ahh.  Modifying locals() is, in general, not supported.  In that case, 
you can always modify globals...

 >>> def f(x):
...     globals()['b'] = x
...     if x == 'x':
...             print b
...     print
 >>> f('y')

 >>> f('x')

In this case, modifying the globals() dict is just like saying:
global b
b = x

But you can do it with a string literal, and without using exec().

  - Josiah

More information about the Python-list mailing list