exec '' in {} what does this do?

Lee Harr missive at frontiernet.net
Tue Nov 5 17:27:14 EST 2002


Hi;

I am reading about type-class unification at:
http://python.org/2.2/descrintro.html

Right near the start, it talks about subclassing builtin types,
but I do not think this question really involves that. It is
more about this piece of code:

    >>> print a.keys()
    [1, 2]
    >>> exec "x = 3; print x" in a
    3
    >>> print a.keys()
    ['__builtins__', 1, 2, 'x']

where a is a dict (well, actually subclassed from dict, but I
tried it with a regular {} and it does the same thing).

What is happening here? I am totally lost.

I thought "in" checked to see if a key was in the dict,
I do not understand why this inserts anything in to the dict.

Any wisdom appreciated.




More information about the Python-list mailing list