Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python closure

def init():
    '''

    :return: increment_num function
    '''
    num = 0

    def increment_num():
        '''
        increments a number by one each call
        :return: num + 1
        '''
        nonlocal num
        num += 1
        return num

    return increment_num


increment = init()

print(increment())
print(increment())

#  output 1 2
Comment

closure python

# Closure in Python. Criteria include 
# 1. must have a nested function
# 2. nested function must refer to a value defined in the enclosing outer function
# 3. enclosing function must return a reference of nested function
# consequence: the variable in the enclosing scope is remembered even when the 
# variable goes out of scope or the function itself is removed from the current namespace.

# Illustration
def counter():
    count = 0
    def increment():
        nonlocal count
        count += 1
        return count
    return increment        # return reference to inner function

instance = counter()    # counter() function is out of namespace after this line
print(instance())       # but counter() function's inner function still works
print(instance())		# and count variable doesn't get refreshed to 0
print(instance())
# 1
# 2
# 3

# Pros: Closures can avoid the use of global values and provides some form of data hiding.
# When there is one method to be implemented in a class, closures can provide an 
# alternate and more elegant solution.
def make_multiplier_of(n):
    def multiplier(x):
        return x * n
    return multiplier

times3 = make_multiplier_of(3)  # Multiplier of 3
times5 = make_multiplier_of(5)  # Multiplier of 5

print(times3(9))
# Output: 27

print(times5(3))
# Output: 15

print(times5(times3(2)))
# Output: 30
Comment

closures in python

def make_counter():

    count = 0
    def inner():

        nonlocal count
        count += 1
        return count

    return inner
Comment

closures in python

def make_summer():

    data = []

    def summer(val):

        data.append(val)
        _sum = sum(data)

        return _sum

    return summer
Comment

closure python

# Closure in Python. Criteria include 
# 1. must have a nested function
# 2. nested function must refer to a value defined in the enclosing outer function
# 3. enclosing function must return a reference of nested function
# consequence: inner function that remembers the variables in its enclosing scope
# even when the outer function is done executing, the variable goes out of scope
# or the function itself is removed from the current namespace. 

# Illustration
def counter():
    count = 0
    def increment():
        nonlocal count
        count += 1
        return count
    return increment        # return reference to inner function

instance = counter()    # counter() function is removed from namespace after this line
print(instance())       # but counter() function's inner function still works
print(instance())       # and it remembers count variable, i.e. it does not get refreshed to 0
print(instance())
# 1
# 2
# 3

# Pros: Closures can avoid the use of global values and provides some form of data hiding.
# When there is one method to be implemented in a class, closures can provide an 
# alternate and more elegant solution.
def make_multiplier_of(n):
    def multiplier(x):
        return x * n
    return multiplier

times3 = make_multiplier_of(3)  # Multiplier of 3
times5 = make_multiplier_of(5)  # Multiplier of 5

print(times3(9))
# Output: 27

print(times5(3))
# Output: 15

print(times5(times3(2)))
# Output: 30

# Python decorators use closure extensively, as it takes a function as parameter
# adds functionality to it and returns the original function. 
Comment

PREVIOUS NEXT
Code Example
Python :: python repeting timer 
Python :: chrome driver in python selenium not working 
Python :: django hash password 
Python :: dataframe move row up one 
Python :: plot size 
Python :: how to make a program that identifies positives and negatives in python 
Python :: set header in dataframe 2nd line 
Python :: python split list into n sublists 
Python :: how to connect wifi using python 
Python :: python list length 
Python :: remove string punctuation python 3 
Python :: get instance of object python 
Python :: read from text file and append in list 
Python :: How to scale a pandas dataframe 
Python :: django create new project 
Python :: append in a for loop python 
Python :: python to excel 
Python :: python append to 2d array 
Python :: how to run linux command in python 
Python :: find max length in string in pandas dataframe 
Python :: change shortcuts in pychar, 
Python :: replace multiple values in pandas column 
Python :: django error table already exists 
Python :: hasattr in python 
Python :: ejercicios con funciones en python 
Python :: convert list to nd array 
Python :: print 1to 10 number without using loop in python 
Python :: how to open a website using python 
Python :: difference between __str__ and __repr__ 
Python :: how to get the local time in python 
ADD CONTENT
Topic
Content
Source link
Name
2+2 =