Created
February 17, 2017 04:08
-
-
Save Siva-Karthi/2ac0cb81bd7515d829edee9ecbd6a93b to your computer and use it in GitHub Desktop.
closure
This file contains 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
#https://www.programiz.com/python-programming/closure | |
def print_msg(msg): | |
# This is the outer enclosing function | |
def printer(): | |
# This is the nested function | |
print(msg) | |
printer() | |
# We execute the function | |
# Output: Hello | |
print_msg("Hello") | |
def print_msg(msg): | |
# This is the outer enclosing function | |
def printer(): | |
# This is the nested function | |
print(msg) | |
return printer # this got changed | |
# Now let's try calling this function. | |
# Output: Hello | |
another = print_msg("Hello") | |
another() | |
del print_msg | |
another() | |
def make_multiplier_of(n): | |
def multiplier(x): | |
return x * n | |
return multiplier | |
# Multiplier of 3 | |
times3 = make_multiplier_of(3) | |
# Multiplier of 5 | |
times5 = make_multiplier_of(5) | |
# Output: 27 | |
print(times3(9)) | |
# Output: 15 | |
print(times5(3)) | |
# Output: 30 | |
print(times5(times3(2))) | |
print make_multiplier_of.__closure__ | |
print times3.__closure__ | |
print times3.__closure__[0].cell_contents | |
print times5.__closure__[0].cell_contents |
This file contains 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
#https://www.programiz.com/python-programming/decorator | |
def first(msg): | |
print(msg) | |
first("Hello") | |
second = first | |
second("Hello") | |
def inc(x): | |
return x + 1 | |
def dec(x): | |
return x - 1 | |
def operate(func, x): | |
result = func(x) | |
return result | |
print operate(inc,3) | |
print operate(dec,3) | |
def is_called(): | |
def is_returned(): | |
print("Hello") | |
return is_returned | |
new = is_called() | |
#Outputs "Hello" | |
new() | |
def make_pretty(func): | |
def inner(): | |
print("I got decorated") | |
func() | |
return inner | |
def ordinary(): | |
print("I am ordinary") | |
ordinary() | |
pretty = make_pretty(ordinary) | |
pretty() | |
pretty = make_pretty(ordinary) | |
ordinary = make_pretty(ordinary) | |
@make_pretty | |
def ordinary(): | |
print("I am ordinary") | |
def ordinary(): | |
print("I am ordinary") | |
ordinary = make_pretty(ordinary) | |
def divide(a, b): | |
return a/b | |
divide(2,5) | |
divide(2,0) | |
def smart_divide(func): | |
def inner(a,b): | |
print("I am going to divide",a,"and",b) | |
if b == 0: | |
print("Whoops! cannot divide") | |
return | |
return func(a,b) | |
return inner | |
@smart_divide | |
def divide(a,b): | |
return a/b | |
divide(2,5) | |
divide(2,0) | |
def works_for_all(func): | |
def inner(*args, **kwargs): | |
print("I can decorate any function") | |
return func(*args, **kwargs) | |
return inner | |
def star(func): | |
def inner(*args, **kwargs): | |
print("*" * 30) | |
func(*args, **kwargs) | |
print("*" * 30) | |
return inner | |
def percent(func): | |
def inner(*args, **kwargs): | |
print("%" * 30) | |
func(*args, **kwargs) | |
print("%" * 30) | |
return inner | |
@star | |
@percent | |
def printer(msg): | |
print(msg) | |
printer("Hello") | |
@star | |
@percent | |
def printer(msg): | |
print(msg) | |
def printer(msg): | |
print(msg) | |
printer = star(percent(printer)) | |
@percent | |
@star | |
def printer(msg): | |
print(msg) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment