<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
<title></title>
</head>
<body bgcolor="#ffffff" text="#000000">
Jordan Greenberg wrote:<br>
<blockquote cite="mid46195808.5040101@gmail.com" type="cite">
<pre wrap="">Andreas Pfrengle wrote:
</pre>
<blockquote type="cite">
<pre wrap="">Bob Gailer wrote:
</pre>
<blockquote type="cite">
<pre wrap="">Andreas Pfrengle wrote:
</pre>
<blockquote type="cite">
<pre wrap="">Hello,
I want to change the value of a variable whose name I don't know, but
this name is stored as a string in another variable, like:
x = 1
var = 'x'
Now I want to change the value of x, but address it via var.
</pre>
</blockquote>
<pre wrap="">exec is the statement for doing this, but the need to do this can
always be met better by using a dictionary instead of global variables.
</pre>
</blockquote>
<pre wrap="">Thanks Bob, the 'exec' saved me. But I'm not sure how I could solve my
specific problem with a dict, since my 'var' variable is a string I'm
getting from a database-field, that refers to the name of another field,
which should be changed. So I see no other way than resolving the string
in some instance - or is there?
</pre>
</blockquote>
<pre wrap=""><!---->
Sure, you do something like:
mydict={'x':1, 'y':2}
var='x'
mydict[var]=5
and then you just access it like mydict['x'] instead of just x.
Jordan
</pre>
</blockquote>
<tt>Hi Jordan,<br>
looks good if I'm happy with my values inside mydict and don't want to
have sth. like x=5 in the end. But since 'x' is the name of a database
field (I simplified it here for an example), I still see no way around
the exec, so I can change the content of the x-field (consider the
content of the var-field as a vector to the corresponding field that
needs a change, x in the example).<br>
If this is still possible with a dict, I still don't see it (sorry),
but it would surely be more elegant than an exec-solution, since I
don't need security checks for the string that is saved, before
executing it.<br>
Andreas<br>
</tt>
</body>
</html>