18
loading...
This website collects cookies to deliver better user experience
def calculation(number):
def double(no):
return no*2
return double(number)
calculation(10) # 20
calculation()
which takes in a number
as argument. Inside it, I have declared another function double()
which also takes in an argument no
and then returns the double of no
. Finally, I exited from the calculation()
function by making a call to the double()
function.calculation()
with parameter value 10
.calculation()
and finds a return
statement with call to another function double()
with the same parameter number = 10
. Current state is paused.double()
with parameter no = 10
and then returns the double of no
which is 20
. Exits the double()
function.20
gets reflected and the paused state is resumed. This results in us finally getting the value 20
.return
statement. The following example demonstrates it:def calculation(number):
def double(no): # executed
return no*2
def triple(no): # skipped
return no*3
def quadruple(no): # executed
return no*4
return double(number), quadruple(number)
calculation(10) # (20, 40) <-- this is a tuple
def parent():
def child()
pass
pass
parent
is the enclosing scope for child
. Which means that child
will only be invoked if parent
makes a call for it. Otherwise, child
will stay dormant.outside1 = "Outside parent func"
def parent(msg):
inside1 = "I am inside parent"
def child()
pass
inside2 = "I am inside parent"
pass
outside2 = "Outside parent func"
parent("Hello there!")
outside1
and outside2
are not inside parent()
therefore, they are not inside the enclosing scope of child()
. On the other hand, inside1
and inside2
are inside parent()
function, this means that they are within the enclosing scope of child()
.child()
? Yes, the msg
variable. Since we can use msg
inside the parent()
function, we can successfully call it to be in the enclosing scope of child()
.def calculation(number):
number2 = 30
def double(no):
return no*2
number3 = 50
return double(number)
calculation(10) # 20
number
, number2
, number3
are in the enclosing scope of double()
. Now, let me remind you, closure property states that: function objects can remember values in its enclosing scopes and these values can be used within that function.
double()
can easily be accessed inside double and be used in any way you want without even passing them as the parameters to double()
function. Didn't get this? Check out the valid snippet below:def calculation(number):
number2 = 30
def double():
print(f"I can remember: {number},{number2},{number3}")
number3 = 50
return double()
calculation(10) # I can remember: 10,30,50
double()
can access and modify number
, number2
, number3
without them being passed as arguments. Well, this is the due to it being able to "remember" the values in its enclosing scope. This was the great story of Closure.def calculation(number):
def double(): # executed
return number*2
def triple(): # skipped
return number*3
def quadruple(): # executed
return number*4
return double(), quadruple()
calculation(10) # (20, 40) <-- this is a tuple