Python - Introduction Scope nesting

Introduction

Scopes may nest arbitrarily, enclosing function def statements are searched when names are referenced:

Demo

def f1(): 
    x = 99 #   w w  w .jav a2s  .  co m
    def f2(): 
        def f3(): 
            print(x)        # Found in f1's local scope! 
        f3() 
    f2() 

f1()

Result

Related Topic