Cannot change global variables in a function through an exec() statement?

前端 未结 3 590
夕颜
夕颜 2020-12-05 23:15

Why can I not change global variables from inside a function, using exec()? It works fine when the assignment statement is outside of exec(). Here is an example of my proble

3条回答
  •  悲&欢浪女
    2020-12-06 00:14

    To add to Alex's answer: although when you omit the locals/globals arguments they default to the locals and globals of the caller, this only a convenience hack; it does not mean they are inheriting the full execution context of the caller. In particular:

    a. nested scope cells are not available to the execed code. So this fails:

    def f():
        foo= 1
        def g():
            exec('print foo')
        g()
    f()
    

    b. global declarations do not carry over into the execed code. So by default as in your example, written variables are put in the locals dictionary. However, you could make it work by saying

    exec('global myvar\nmyvar = "changed!"')
    

    You don't really want to be doing this if you can help it. global already isn't nice and exec is pretty much a code smell in itself! You wouldn't want to combine them unless there was really no alternative.

提交回复
热议问题