Created
May 20, 2021 07:18
-
-
Save AlekseiCherkes/d8ad18f34e48a8b767a2a1f2c2361e52 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# 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