Python Tutorial

Tuesday, February 19, 2013

Closure in python

Closure is a function that defined inside another function. Closure function can be return outside, then other code can use it. Example given below:

All source code available on github

def value(x):
    def increment_by(y):
        return x+y
    return increment_by

increment10 = value(10)
print increment10(5)

increment45 = value(45)
print increment45(5)


Output:
15
50

0 comments:

Post a Comment