Skip to content

Commit b9eafb0

Browse files
authored
added nested function & closure👩‍💻
1 parent a0be7a3 commit b9eafb0

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
def nth_power(exponent ):
3+
def pow_of(base):
4+
return pow(base, exponent)
5+
return pow_of # note that we are returning function without function
6+
7+
square = nth_power(2)
8+
print(square(31))
9+
print(square(32))
10+
print(square(33))
11+
print(square(34))
12+
print(square(35))
13+
print(square(36))
14+
print(square(37))
15+
print(square(38))
16+
print(square(39))
17+
18+
print('------')
19+
20+
cube = nth_power(3)
21+
print(cube(2))
22+
print(cube(3))
23+
print(cube(4))
24+
print(cube(5))
25+
print(cube(6))
26+
print(cube(7))
27+
print(cube(8))
28+
print(cube(9))
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# nested function u can define function inside a fucntion
2+
3+
# closures are sometimes uses in the placeof classes which usually have only one method or few method . Closures are highly used in decorators
4+
5+
def outterFunction(text):
6+
def innerFunction():
7+
print(text)
8+
return innerFunction # to make closure fucntion from nested function you have to return function without paranthesis
9+
10+
a = outterFunction("Hello")
11+
del outterFunction
12+
a() # after deleting outterfunction o/p - Hello means a variable is sotring some spaces for innerfunction even outterfunction is deleted this is magic of 'closure'
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
def pop(list):
2+
def get_last_item(my_list):
3+
return my_list[len(list) - 1]
4+
5+
list.remove(get_last_item(list))
6+
return list
7+
8+
a = [1,2,3,4,5]
9+
print(pop(a))
10+
print(pop(a))
11+
print(pop(a))

0 commit comments

Comments
 (0)