scope of eval function in python

后端 未结 2 1432
失恋的感觉
失恋的感觉 2021-01-01 13:52

Consider the following example:

i=7
j=8
k=10
def test():
    i=1
    j=2
    k=3
    return dict((name,eval(name)) for name in [\'i\',\'j\',\'k\'])
         


        
2条回答
  •  一个人的身影
    2021-01-01 14:31

    Generators are implemented as function scopes:

    The scope of names defined in a class block is limited to the class block; it does not extend to the code blocks of methods – this includes generator expressions since they are implemented using a function scope.

    So, the generator inside the dict() constructor has its own locals() dictionary. Now let's take a look at Py_eval's source code, specially when both globals() and locals() are None:

    if (globals == Py_None) {
            globals = PyEval_GetGlobals();
            if (locals == Py_None)
                locals = PyEval_GetLocals();
        }
    

    So, for your example PyEval_GetLocals() will be empty at the moment the loop is executing and globals() will be the global dictionary. Note that i, j and k defined inside the function are not in local scope of generator, rather they are in its enclosing scope:

    >>> dict((name,eval(name, globals(), {})) for name in ['i', 'j', 'k'])
    {'i': 7, 'k': 10, 'j': 8}
    

提交回复
热议问题