Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

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
Source by codefreelance.net #
 
PREVIOUS NEXT
Tagged: #closure #python
ADD COMMENT
Topic
Name
9+1 =