Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A difference with "exec" that is not documented #37

Open
LRGH opened this issue May 19, 2015 · 1 comment
Open

A difference with "exec" that is not documented #37

LRGH opened this issue May 19, 2015 · 1 comment

Comments

@LRGH
Copy link

LRGH commented May 19, 2015

The following piece of code

    def u():
        exec("a=2")
        print(a)
    u()

prints "2" with python2 but "NameError: name 'a' is not defined" with python3.

The way to have it work with both versions is:

    def u():
        exec("a=2")
        print(locals()['a'])
    u()
@LRGH
Copy link
Author

LRGH commented May 20, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant