为什么不能在函数中使用' import * ' ?

时间:2021-02-28 06:53:16

This works as expected

这是意料之中的

def outer_func():
    from time import *

    print time()

outer_func()

I can define nested functions in the context fine and call them from other nested functions:

我可以在context fine中定义嵌套函数,并从其他嵌套函数中调用:

def outer_func():
    def time():
        return '123456'

    def inner_func():
        print time()

    inner_func()

outer_func()

I can even import individual functions:

我甚至可以导入各个功能:

def outer_func():
    from time import time

    def inner_func():
        print time()

    inner_func()

outer_func()

This, however, throws SyntaxError: import * is not allowed in function 'outer_func' because it contains a nested function with free variables:

然而,这将抛出SyntaxError:在函数'outer_func'中不允许导入*,因为它包含一个带有*变量的嵌套函数:

def outer_func():
    from time import *

    def inner_func():
        print time()

    inner_func()

outer_func()

I'm aware this isn't best practice, but why doesn't it work?

我知道这不是最好的做法,但为什么它不奏效呢?

1 个解决方案

#1


22  

The compiler has no way of knowing whether the time module exports objects named time.

编译器无法知道time模块是否导出名为time的对象。

The free variables of nested functions are tied to closure cells at compile time. Closure cells themselves point to (local) variables defined in compiled code, as opposed to globals, which are not tied at all. See the python data model; functions refer to their globals via the func_globals attribute, and the func_closure attribute holds a sequence of closure cells (or None).

嵌套函数的*变量在编译时绑定到闭包单元。闭包单元格本身指向已编译代码中定义的(局部)变量,而不是全局变量,它们根本不绑定。参见python数据模型;函数通过func_globals属性引用它们的全局变量,func_closure属性包含一个闭包单元序列(或None)。

As such, you cannot use a dynamic import statement in a nested scope.

因此,不能在嵌套的范围内使用动态导入语句。

And why do nested functions need closure cells at all? Because you need a mechanism to refer to local function variables when the function itself has finished:

为什么嵌套函数需要闭包单元格呢?因为当函数本身完成时,需要一种机制来引用局部函数变量:

def foo(spam):
    def bar():
        return spam
    return bar

afunc = foo('eggs')

By calling foo() I obtained a nested function that refers to a scoped variable, and the compiler needs to create the necessary references for the interpreter to be able to retrieve that scoped variable again. Hence the cells, and the limitations placed on them.

通过调用foo(),我获得了一个嵌套函数,它引用一个作用域的变量,编译器需要为解释器创建必要的引用,以便能够再次检索这个作用域的变量。因此有了单元格,以及对它们的限制。

#1


22  

The compiler has no way of knowing whether the time module exports objects named time.

编译器无法知道time模块是否导出名为time的对象。

The free variables of nested functions are tied to closure cells at compile time. Closure cells themselves point to (local) variables defined in compiled code, as opposed to globals, which are not tied at all. See the python data model; functions refer to their globals via the func_globals attribute, and the func_closure attribute holds a sequence of closure cells (or None).

嵌套函数的*变量在编译时绑定到闭包单元。闭包单元格本身指向已编译代码中定义的(局部)变量,而不是全局变量,它们根本不绑定。参见python数据模型;函数通过func_globals属性引用它们的全局变量,func_closure属性包含一个闭包单元序列(或None)。

As such, you cannot use a dynamic import statement in a nested scope.

因此,不能在嵌套的范围内使用动态导入语句。

And why do nested functions need closure cells at all? Because you need a mechanism to refer to local function variables when the function itself has finished:

为什么嵌套函数需要闭包单元格呢?因为当函数本身完成时,需要一种机制来引用局部函数变量:

def foo(spam):
    def bar():
        return spam
    return bar

afunc = foo('eggs')

By calling foo() I obtained a nested function that refers to a scoped variable, and the compiler needs to create the necessary references for the interpreter to be able to retrieve that scoped variable again. Hence the cells, and the limitations placed on them.

通过调用foo(),我获得了一个嵌套函数,它引用一个作用域的变量,编译器需要为解释器创建必要的引用,以便能够再次检索这个作用域的变量。因此有了单元格,以及对它们的限制。