Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
oorrja
GitHub Repository: oorrja/learntosolveit
Path: blob/master/languages/python/design_closure1.py
1240 views
1
"""
2
A Simple Example of closure in python
3
A closure is function which returns another function. For example, the
4
``constant`` function here returns, ``_inner`` function. At the top level you
5
passed the value and calling the inner function from within, it is not required
6
to send the value.
7
"""
8
def constant(value):
9
def sq(x):
10
return x*x
11
12
def _inner():
13
return sq(value)
14
return _inner
15
16
x = constant(5)
17
print((x()))
18
19