>
> What is really weird is that the class body is evaluated without
> considering the enclosed scope, but the methods defined in the class
> have access to the enclosed scope.

Found the culprit. Lets looks at the following code.

code = """
x = x+1
def f():
    return x
print(x, f())
"""

x = 5
exec(code)

x = 5
exec(code, globals(), {})

When exec is called without any locals, it works as we expected and x
in f is bound to the x in the code.

But when we supply a locals dict to exec, it behaves differently. The
x in f is bound to global x.

I'm not sure why we have this confusing behavior.

--
Anand
http://anandology.com/
_______________________________________________
BangPypers mailing list
BangPypers@python.org
http://mail.python.org/mailman/listinfo/bangpypers

Reply via email to