TPJ wrote:
(...) Even allowing for the
difficulties you've already experienced, it's nearly always better in
practical cases to use assignment to the keys of a dictionary. Then no
exec is required, and you have direct control over your own namespace.
Well... Is this a sugestion, that
I have the following code:
---
def f():
def g():
a = 'a' # marked line 1
exec 'a = b' in globals(), locals()
print g: a =, a
a = 'A' # marked line 2
exec 'a = B' in globals(), locals()
print f: a =, a
g()
f()
TPJ wrote:
I have the following code:
---
def f():
def g():
a = 'a' # marked line 1
exec 'a = b' in globals(), locals()
print g: a =, a
a = 'A' # marked line 2
exec 'a = B' in globals(), locals()
print
TPJ wrote:
I have the following code:
---
def f():
def g():
a = 'a' # marked line 1
exec 'a = b' in globals(), locals()
print g: a =, a
a = 'A' # marked line 2
exec 'a = B' in globals(), locals()
print f:
Use the exec statement without the in-clause to get the desired effect:
def f():
... a = a
... exec a = 'B'
... print a
...
f()
B
snip
Well... I *do* realize that. But this is *not* my problem. I have a
function with another nested one. If I used exec ... instead of exec
- by using a
dictionary. And perhaps I will. But I still want to know, how the exec
statement works.
* * *
My problem is more complicated, that the presented example. In general,
my problem is: how to create a local variable by executing the Python
code, that isn't known at the moment of writing