[Tutor] importing into a function
Dave Angel
davea at davea.name
Thu Jul 11 00:50:50 CEST 2013
On 07/10/2013 05:16 PM, eryksun wrote:
> On Wed, Jul 10, 2013 at 3:57 PM, Dave Angel <davea at davea.name> wrote:
>>>
>>> Sure enough it did. The question is - does importing into a function
>>> ever make sense? It occurred to me that if you use from module
>>> import * , putting it in a function
>>
>> Can't be done. Locals of a function are all known at compile time.
>
> That depends. 2.x issues a warning and disables optimization, so the
> function's locals use a dict:
>
> >>> from inspect import CO_OPTIMIZED
> >>> def f(): from Tkinter import *
> ...
> <stdin>:1: SyntaxWarning: import * only allowed at module level
> >>> f.__code__.co_flags & CO_OPTIMIZED
> 0
>
> I'm not saying this is a good idea. It isn't compatible with 3.x, you
> lose the ability to create a closure (i.e. a nested function with free
> variables), you lose the efficiency of the fast locals array, and
> every call has to create and update a locals dict.
All good points. But Jim Mooney is running Python 3.3, as evidenced by
the location he gives for the interpreter.
--
DaveA
More information about the Tutor
mailing list