python - How does this function find the value of another variable? -


here code:

def caller(callee):     callee()  def wrapper():      def a():         print v0      in range(5):         v0 = i*i         caller(a)  wrapper() 

the above code outputs:

0 1 4 9 16 

but don't understand how a resolves variable v0. can not find related python docs regarding language feature.

the scope of variables defined in function includes nested functions within it. variables defined in wrapper() accessible within a(), because function nested in it. known lexical scoping, , it's used in creating closures.

this explained in python resolution of names documentation:

a scope defines visibility of name within block. if local variable defined in block, scope includes block. if definition occurs in function block, scope extends blocks contained within defining one, unless contained block introduces different binding name.


Comments

Popular posts from this blog

java - Suppress Jboss version details from HTTP error response -

gridview - Yii2 DataPorivider $totalSum for a column -

Sass watch command compiles .scss files before full sftp upload -