Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR 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
Source by codefreelance.net #
 
PREVIOUS NEXT
Tagged: #python #closure
ADD COMMENT
Topic
Name
6+1 =