Skip to content

Instantly share code, notes, and snippets.

@AlekseiCherkes
Created May 20, 2021 07:18
Show Gist options
  • Save AlekseiCherkes/d8ad18f34e48a8b767a2a1f2c2361e52 to your computer and use it in GitHub Desktop.
Save AlekseiCherkes/d8ad18f34e48a8b767a2a1f2c2361e52 to your computer and use it in GitHub Desktop.
#
# Closure
#
def foo():
x = 123
y = [1, 2, 3]
z = [1, 2, 3]
def c1():
print(x)
print(y)
print(z)
x = 321
y = [3, 2, 1]
z.clear()
z.append(3)
z.append(2)
z.append(1)
def c2():
print(x)
print(y)
print(z)
return c1, c2
#
# Test
#
cc1, cc2 = foo()
print("=== c1 ===")
cc1()
print("=== c2 ===")
cc2()
# === c1 ===
# 321
# [3, 2, 1]
# [3, 2, 1]
# === c2 ===
# 321
# [3, 2, 1]
# [3, 2, 1]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment