Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I use `import *` in a function?

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:

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:

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?

like image 693
Wilfred Hughes Avatar asked Nov 19 '12 15:11

Wilfred Hughes


People also ask

Can I import inside a function?

Most of the time if you can do something in a function, you can do it in global scope or in a class definition or vice versa. There's an exception for from ... import * , which works at global scope but not inside a function.

Can I use import inside a function in Python?

Importing inside a function will effectively import the module once.. the first time the function is run. It ought to import just as fast whether you import it at the top, or when the function is run.

What is the difference between import and from Importstatement?

Explain the difference between import and from import statement, with example. It import entire module and so everything inside the module will be imported like functions, constant, variables. from <module> import statement imports selected items , but to use these items we don't have to prefix module name.


1 Answers

The compiler has no way of knowing whether the time module exports objects named 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).

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.

like image 79
Martijn Pieters Avatar answered Oct 05 '22 15:10

Martijn Pieters