[Python-bugs-list] [ python-Bugs-440497 ] nested function with variables

noreply@sourceforge.net noreply@sourceforge.net
Wed, 11 Jul 2001 13:24:05 -0700


Bugs item #440497, was opened at 2001-07-11 13:16
You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=440497&group_id=5470

Category: None
Group: None
Status: Open
Resolution: None
Priority: 5
Submitted By: Nobody/Anonymous (nobody)
Assigned to: Nobody/Anonymous (nobody)
Summary: nested function with variables

Initial Comment:
This code should not result in a syntax warning:

def t0():
    from math import *
    def t1(): print map(lambda x:x,[])
    def t2(): print None

SyntaxWarning: import * is not allowed in function 't0'
because it contains a nested function with free
variables

Moving the function body one level out results in no
warnings, removing the import as well. But the warnings
persist for both t1 and t2 by itself.

I have been trying to port my code to Python-2.1 (from
2.0)
and encountered numerous problems due with
SyntaxWarnings regarding the new scoping rules that I
found way too confusing to be able to track it for my
large code.

These syntax warnings should be one of the major
functions of 2.1 since they should help the migration
to 2.2 with the new scoping enabled ......



----------------------------------------------------------------------

>Comment By: Guido van Rossum (gvanrossum)
Date: 2001-07-11 13:24

Message:
Logged In: YES 
user_id=6380

You get the warning because the compiler doesn't know
whether the math module exports objects named 'map' or
'None'. (This may be obvious to you, but the compiler isn't
clairvoyant, and it can't go and import the module while
it's compiling another module.)

There are several ways to avoid the warning: 

1) Move the import to the module level. (Why are you putting
the import in the function? It's slower that way.)

2) Be specific about the functions you import, e.g. write
    from math import sin, cos, tan

3) Use "import math" and write "math.sin(x)" etc. rathern
than "sin(x)".

Your example doesn't show why you would write such code.
Perhaps showing some real code that you use that runs into
this problem can give a better insight in how to deal with
your porting problem...


----------------------------------------------------------------------

You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=440497&group_id=5470