Python - Introduction Scope nesting

Introduction

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

Demo

def f1(): 
    x = 99 #  ww w.ja v  a2  s .com
    def f2(): 
        def f3(): 
            print(x)        # Found in f1's local scope! 
        f3() 
    f2() 

f1()

Result

Related Topic