This commit is contained in:
2024-09-19 12:43:22 +02:00
parent 83ad73555d
commit bc4c310e2c
3 changed files with 95 additions and 0 deletions

View File

@ -0,0 +1,51 @@
# A Closure is a function object that remembers values in enclosing scopes even if they are not present in memory. Let us get to it step by step
# Firstly, a Nested Function is a function defined inside another function. It's very important to note that the nested functions can access the variables of the enclosing scope. However, at least in python, they are only readonly. However, one can use the "nonlocal" keyword explicitly with these variables in order to modify them.
def transmit_to_space(message):
"This is the enclosing function"
def data_transmitter():
"The nested function"
print(message)
data_transmitter()
print(transmit_to_space("Test message"))
# This works well as the 'data_transmitter' function can access the 'message'. To demonstrate the use of the "nonlocal" keyword, consider this
def print_msg(number):
def printer():
"Here we are using the nonlocal keyword"
nonlocal number
number=3
print(number)
printer()
print(number)
print_msg(9)
# Without the nonlocal keyword, the output would be "3 9", however, with its usage, we get "3 3", that is the value of the "number" variable gets modified.
# Now, how about we return the function object rather than calling the nested function within. (Remember that even functions are objects. (It's Python.))
def transmit_to_space(message):
"This is the enclosing function"
def data_transmitter():
"The nested function"
print(message)
return data_transmitter
fun2 = transmit_to_space("Burn the Sun!")
fun2()
# Exercise
# Make a nested loop and a python closure to make functions to get multiple multiplication functions using closures. That is using closures, one could make functions to create multiply_with_5() or multiply_with_4() functions using closures.
def multiplier_of(n):
def multiplier(number):
print("n:",n)
print("number:", number)
return number*n
return multiplier
multiplywith5 = multiplier_of(5)
print(multiplywith5(9))

View File

@ -0,0 +1,43 @@
# Decorators allow you to make simple modifications to callable objects like functions, methods, or classes. We shall deal with functions for this tutorial. The syntax
# @decorator
# def functions(arg):
# return "value"
# #Is equivalent to:
# def function(arg):
# return "value"
# function = decorator(function) # this passes the function to the decorator, and reassigns it to the functions
# function(5)
def repeater(old_function):
def new_function(*args, **kwds): # See learnpython.org/en/Multiple%20Function%20Arguments for how *args and **kwds works
old_function(*args, **kwds) # we run the old function
old_function(*args, **kwds) # we do it twice
return new_function # we have to return the new_function, or it wouldn't reassign it to the value
@repeater
def multiply(num1, num2):
print(num1 * num2)
multiply(2, 3)
def multiply(multiplier):
def multiply_generator(old_function):
def new_function(*args, **kwds):
return multiplier * old_function(*args, **kwds)
return new_function
return multiply_generator # it returns the new generator
# Usage
@multiply(3) # multiply is not a generator, but multiply(3) is
def return_num(num):
return num
# Now return_num is decorated and reassigned into itself
return_num(5) # should return 15

View File

@ -0,0 +1 @@
# This was too advanced for me at least for now! So Ill just leave the URL here: https://www.learnpython.org/en/Map,_Filter,_Reduce