[Zope] [python] creating variable names by adding 2 strings?

Dieter Maurer dieter@handshake.de
Mon, 22 Jan 2001 19:46:48 +0100 (CET)


Lee writes:
 > I'm creating variables in python but I am having trouble creating them
 > when they're *named* using other variables. Here's an example;
 > 
 > while (p!=0):
 >      	p+`p`= string.replace(component[control], ",", "") 
 > 	# e.g. I want 'p1 = string.replace.blah...'
 >     	p=p-1
 >     	control=control+1
 > 
 > ==> SyntaxError: can't assign to operator :(
Why do you want to do that?

  It is really strange, that you want computed variable names
  in local namespaces.

If you want them in an object, class or module, you could use
"setattr". If it should go into the current module,
you could use "globals()[name_expr]=value" (though the
Python spec says, this may change in future implementations).

Note, that security restrictions may prevent you from
using "setattr" in Python scripts.


Dieter