isPrime works but UnBoundLocalError when mapping on list

Fredrik Lundh fredrik at pythonware.com
Wed Jul 16 11:28:58 CEST 2008


Andreas Tawn wrote:

> I never knew that and I can't find reference to it in the docs.

the for-in loop does ordinary assignments in the current scope:

     http://docs.python.org/ref/for.html

     "Each item in turn is assigned to the target list using the
     standard rules for assignments, and then the suite is executed."

somewhat simplified, "for vars in expression: code" is equivalent to 
inlining:

     _ = iter(expression)
     while 1:
         try:
             vars = _.next()
         except StopIteration:
             break
         else:
             body

where "_" is an internal variable.

</F>




More information about the Python-list mailing list