scoping problem in recursive closure

Posted by wiso on Stack Overflow See other posts from Stack Overflow or by wiso
Published on 2010-03-25T15:07:43Z Indexed on 2010/03/25 15:33 UTC
Read the original article Hit count: 338

Filed under:
|
|
|

why this work:

def function1():                                                                                                             
       a = 10                                                                                                                    
       def function2():
          print a
       function2()

but this not:

def function1():
    a = 10
    def function2():
        print a
        a -= 1
        if a>0:
           function2()
    function2()

error:

UnboundLocalError: local variable 'a' referenced before assignment

© Stack Overflow or respective owner

Related posts about python

Related posts about closure