469,572 Members | 1,718 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,572 developers. It's quick & easy.

exec example - I don't understand

Hi !

In
Unifying types and classes in Python 2.2

article I see that:
print a # show the result {1: 3.25, 2: 200}
We can also use the new type in contexts where classic only allows
"real" dictionaries, such as the locals/globals dictionaries for the
exec statement or the built-in function eval():
print a.keys() [1, 2] exec "x = 3; print x" in a

3

But I dont' understand that:
exec "x = 3; print x" in a

So what this code do ?
Why we need "in a" ?

This get same result !

Thanx for help:
FT

Jul 18 '05 #1
3 3751
ke*************@peto.hu wrote:
But I dont' understand that:
exec "x = 3; print x" in a

So what this code do ?
Why we need "in a" ?

This get same result !


First we make sure there's no variable x in the global namespace:
x Traceback (most recent call last):
File "<stdin>", line 1, in ?
NameError: name 'x' is not defined

Underneath, the global namespace is just a dictionary with the variable
names as keys and the objects as values. "x" in globals() False

Now let's use our own dictionary d as the namespace for the exec statement: d = {}
exec "x=1" in d
This leaves the global namespace unaffected: "x" in globals() False

Instead 1 is stored under the key "x" in the dictionary we provided: "x" in d True d["x"] 1

Now let's repeat the same exec statement without explicitly providing a
dictionary. Python will then use globals() as the default. Therefore a
variable x with the value 1 will "magically" appear: exec "x=1"
"x" in globals() True x

1

Peter

Jul 18 '05 #2
On Tue, 29 Jun 2004, ke*************@peto.hu wrote:
print a # show the result {1: 3.25, 2: 200} print a.keys() [1, 2] exec "x = 3; print x" in a 3

But I dont' understand that:
exec "x = 3; print x" in a

So what this code do ?
Why we need "in a" ?


The 'in a' tells exec to run the code using the dictionary a to read and
store variables. In this case, when x is set equal to 3, it's actually
a['x'] being set to 3. Try these examples to get an idea for what's going
on:
a = {'x': 15}
exec 'print x' in a 15 exec 'print x' NameError: name 'x' is not defined exec 'x*=3; print x' in a 45 x NameError: name 'x' is not defined a['x'] 45 exec 'y=10; print y' in a 10 y NameError: name 'y' is not defined a['y']

10
Jul 18 '05 #3
HI !

Thanx for every answer.
But it is a "bad" thing: when I think to I know something about python I
get some not understanded source code with helpful people's answers, and
them show me that I don't know nothing...

:-)
Christopher T King wrote:
On Tue, 29 Jun 2004, ke*************@peto.hu wrote:
>print a # show the result
>
>

{1: 3.25, 2: 200}

>print a.keys()
>
>

[1, 2]

>exec "x = 3; print x" in a
>
>

3

But I dont' understand that:
exec "x = 3; print x" in a

So what this code do ?
Why we need "in a" ?


The 'in a' tells exec to run the code using the dictionary a to read and
store variables. In this case, when x is set equal to 3, it's actually
a['x'] being set to 3. Try these examples to get an idea for what's going
on:
a = {'x': 15}
exec 'print x' in a

15

exec 'print x'

NameError: name 'x' is not defined

exec 'x*=3; print x' in a

45

x

NameError: name 'x' is not defined

a['x']

45

exec 'y=10; print y' in a

10

y

NameError: name 'y' is not defined

a['y']

10

Jul 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Avi Kak | last post: by
1 post views Thread by Andr? Roberge | last post: by
10 posts views Thread by Antoon Pardon | last post: by
5 posts views Thread by TPJ | last post: by
11 posts views Thread by ...:::JA:::... | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.