Python 3.4, no acces to a variable created with exec()

后端 未结 1 1960
时光说笑
时光说笑 2021-01-26 01:38

With Python 3.4 and in a function, this piece of code appears:

ch0 = \'dico[\\\'\'+ ans +\'\\\']\' # dico is a dictionary and ans is a key of this dictionary

pr         


        
1条回答
  •  无人及你
    2021-01-26 01:50

    As documented in the exec documentation for Python 3:

    Note: The default locals act as described for function locals() below: modifications to the default locals dictionary should not be attempted. Pass an explicit locals dictionary if you need to see effects of the code on locals after function exec() returns.

    You can't create new local variables with exec on Python 3. Local variable lookup completely ignores the attempt.

    0 讨论(0)
提交回复
热议问题