__closure__
def enclosing():
x = 'closed over'
def local_func():
print(x)
return local_func
lf = enclosing()
lf()
#=> closed over
lf.__closure__
#=> (<cell at 0x10ea95cc8: str object at 0x10eac19f0>,)
def raise_to(exp):
def raise_to_exp(x):
return pow(x, exp)
return raise_to_exp
nonlocal
Keywordnonlocal
keyword introduces names from enclosing namespace into local namespace💡 IMPORTANT 💡 using
nonlocal
with a name that doesn't exist will result in error
message = 'global'
def enclosing():
message = 'enclosing'
def local():
# does not affect `global` or `enclosing` bindings
message = 'local'
print('enclosing message before `local()` call: ', message)
local()
print('enclosing message after `local()` call: ', message)
print('global message before `enclosing()` call: ', message)
enclosing()
print('global message after `enclosing()` call: ', message)
#=> global message before `enclosing()` call: global
#=> enclosing message before `local()` call: enclosing
#=> enclosing message after `local()` call: enclosing
#=> global message after `enclosing()` call: global
local
binding using global
keywordmessage = 'global'
def enclosing():
message = 'enclosing'
def local():
global message
message = 'local'
print('enclosing message before `local()` call: ', message)
local()
print('enclosing message after `local()` call: ', message)
print('enclosing message before `enclosing()` call: ', message)
enclosing()
print('enclosing message after `enclosing()` call: ', message)
#=> global message before `enclosing()` call: global
#=> enclosing message before `local()` call: enclosing
#=> enclosing message after `local()` call: enclosing
#=> global message after `enclosing()` call: local
local
binding using nonlocal
keywordmessage = 'global'
def enclosing():
message = 'enclosing'
def local():
nonlocal message
message = 'local'
print('enclosing message before `local()` call: ', message)
local()
print('enclosing message after `local()` call: ', message)
print('enclosing message before `enclosing()` call: ', message)
enclosing()
print('enclosing message after `enclosing()` call: ', message)
#=> global message before `enclosing()` call: global
#=> enclosing message before `local()` call: enclosing
#=> enclosing message after `local()` call: local
#=> global message after `enclosing()` call: global
# make_timer.py
import time
def make_timer():
last_called = None
def elapsed():
nonlocal last_called
now = time.time()
if last_called is None:
last_called = now
return None
result = now - last_called
last_called = now
return result
return elapsed