1

This is an example of an enclosing scope (is it also called lexical scope?):

def outer(a):
    def inner():
        return a
    return inner

if __name__ == '__main__':
    inner = outer(10)
    print(inner())

This code also works:

def printer():
    print(x)

if __name__ == '__main__':
    x = 10
    printer()

My question is: Is the "enclosing scope" rule used in the second example? If so, is there any distinction between the 2 types of enclosing scope: at call point/runtime and at definition?

Edit: as @user2357112 pointed out, x is global. This one doesn't work:

def printer():
    print(x)

def outer(func_print):
    x = 10
    func_print()


if __name__ == '__main__':
    outer(printer)

What a rookie mistake...

Minh Nghĩa
  • 854
  • 1
  • 11
  • 28

0 Answers0