You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# First class objects in a language are handled uniformly throughout.
4
-
# They may be stored in data structures, passed as arguments, or used in control structures.
5
-
# A programming language is said to support first-class functions if it treats functions as first-class objects.
3
+
# First class objects in a language are handled uniformly throughout.
4
+
# They may be stored in data structures, passed as arguments, or used in control structures.
5
+
# A programming language is said to support first-class functions if it treats functions as first-class objects.
6
6
# Python supports the concept of First Class functions.
7
7
8
8
# Properties of first class functions:
@@ -15,113 +15,150 @@
15
15
16
16
# Examples illustrating First Class functions in Python
17
17
18
-
# 1. Functions are objects:
19
-
# Python functions are first class objects.
18
+
# 1. Functions are objects:
19
+
# Python functions are first class objects.
20
20
21
-
# Python program to illustrate functions
22
-
# can be treated as objects
23
-
defshout(text):
24
-
returntext.upper() # Print Uppercase
21
+
# Python program to illustrate functions
22
+
# can be treated as objects
23
+
defshout(text):
24
+
returntext.upper() # Print Uppercase
25
25
26
-
27
-
print (shout('Hello')) # HELLO
28
-
print(shout) # Print the object of the function
29
-
yell=shout# assign a function to a variable and can be treated as objects
30
-
print(yell) # print the object of the function shot and yell are same address yell -->shot
31
-
print (yell('Hello')) # HELLO
26
+
27
+
print(shout("Hello")) # HELLO
28
+
print(shout) # Print the object of the function
29
+
yell=shout# assign a function to a variable and can be treated as objects
30
+
print(
31
+
yell
32
+
) # print the object of the function shot and yell are same address yell -->shot
33
+
print(yell("Hello")) # HELLO
32
34
33
35
# In the above program we are assigning function to a variable
34
36
# This assignment doesn’t call the function. (yell=shot)
35
37
# It takes the function object referenced by shout and creates a second name pointing to it, yell.(yell -->shot)
36
38
39
+
37
40
# Example 2:
38
41
defsquare(x):
39
-
returnx*x
40
-
f=square(5)
41
-
print(square) # print square functions object <function square at 0x7efca930fd90>
42
-
print(f) # 25
43
-
f=square# print the object of the function square and f are same address f -->square
44
-
print(f) # print square functions object <function square at 0x7efca930fd90>
45
-
print(f(5)) # 25
46
-
47
-
48
-
# 2. Functions can be passed as arguments to other functions:
49
-
# Because functions are objects we can pass them as arguments to other functions.
50
-
# Functions that can accept other functions as arguments are also called higher-order functions.
51
-
52
-
# Python program to illustrate functions
53
-
# can be passed as arguments to other functions
54
-
defshout(text): # the function was called from greet and it contains values on it
55
-
returntext.upper()
56
-
57
-
defwhisper(text): # the function was called from greet and it contains values on it
58
-
returntext.lower()
59
-
60
-
defgreet(func):# func - shout it will call the shout function
61
-
# storing the function in a variable
62
-
greeting=func("""Hi, I am created by a function
63
-
passed as an argument.""") # It will pass the argument to the func=shout,whisper
64
-
print (greeting) # print the returned functions value
65
-
66
-
greet(shout) # HI, I AM CREATED BY A FUNCTION PASSED AS AN ARGUMENT.
67
-
greet(whisper) # hi, i am created by a function passed as an argument.
42
+
returnx*x
43
+
44
+
45
+
f=square(5)
46
+
print(square) # print square functions object <function square at 0x7efca930fd90>
47
+
print(f) # 25
48
+
f=square# print the object of the function square and f are same address f -->square
49
+
print(f) # print square functions object <function square at 0x7efca930fd90>
50
+
print(f(5)) # 25
51
+
52
+
53
+
# 2. Functions can be passed as arguments to other functions:
54
+
# Because functions are objects we can pass them as arguments to other functions.
55
+
# Functions that can accept other functions as arguments are also called higher-order functions.
56
+
57
+
58
+
# Python program to illustrate functions
59
+
# can be passed as arguments to other functions
60
+
defshout(text): # the function was called from greet and it contains values on it
61
+
returntext.upper()
62
+
63
+
64
+
defwhisper(text): # the function was called from greet and it contains values on it
65
+
returntext.lower()
66
+
67
+
68
+
defgreet(func): # func - shout it will call the shout function
69
+
# storing the function in a variable
70
+
greeting= (
71
+
func("""Hi, I am created by a function
72
+
passed as an argument.""")
73
+
) # It will pass the argument to the func=shout,whisper
74
+
print(greeting) # print the returned functions value
75
+
76
+
77
+
greet(shout) # HI, I AM CREATED BY A FUNCTION PASSED AS AN ARGUMENT.
78
+
greet(whisper) # hi, i am created by a function passed as an argument.
68
79
69
80
# In the example above, we have created a function greet which takes a function as an argument.
70
81
82
+
71
83
# Example 2:
72
84
defsquare(x):
73
-
returnx*x
85
+
returnx*x
86
+
74
87
75
88
defcube(x):
76
-
returnx*x*x
89
+
returnx*x*x
77
90
78
-
defmyfunc(func,args):
79
-
result=[]
91
+
92
+
defmyfunc(func, args):
93
+
result= []
80
94
foriinargs:
81
95
result.append(func(i))
82
96
returnresult
83
97
84
-
cubes=myfunc(cube,[1,2,3,4,5]) # cube name and list is passed to myfunc and it calls the cube function
85
-
print(cubes) # print cube functions value in list format [1, 8, 27, 64, 125]
86
-
squares=myfunc(square,[1,2,3,4,5]) # square name and list is passed to myfunc and it calls the square function
87
-
print(squares) # print square functions value in list format [1, 4, 9, 16, 25]
88
98
89
-
# 3. Functions can return another function:
99
+
cubes=myfunc(
100
+
cube, [1, 2, 3, 4, 5]
101
+
) # cube name and list is passed to myfunc and it calls the cube function
102
+
print(cubes) # print cube functions value in list format [1, 8, 27, 64, 125]
103
+
squares=myfunc(
104
+
square, [1, 2, 3, 4, 5]
105
+
) # square name and list is passed to myfunc and it calls the square function
106
+
print(squares) # print square functions value in list format [1, 4, 9, 16, 25]
107
+
108
+
# 3. Functions can return another function:
90
109
# Because functions are objects we can return a function from another function.
91
110
92
-
# Python program to illustrate functions
93
-
# Functions can return another function
94
-
95
-
defcreate_adder(x): # x=15
96
-
defadder(y):
97
-
print(x) # x=15 value will be passed inside adder function
98
-
returnx+y
99
-
100
-
returnadder# return the another function (adder)
101
-
102
-
add_15=create_adder(15)
103
-
print(add_15) # <function create_adder.<locals>.adder at 0x7f8aceb8a950> It return the function and locals object
104
-
print (add_15(10)) # It will pass the y=10 value it call the create_adder(15)and call the locals pass the value 10
111
+
# Python program to illustrate functions
112
+
# Functions can return another function
113
+
114
+
115
+
defcreate_adder(x): # x=15
116
+
defadder(y):
117
+
print(x) # x=15 value will be passed inside adder function
118
+
returnx+y
119
+
120
+
returnadder# return the another function (adder)
121
+
122
+
123
+
add_15=create_adder(15)
124
+
print(
125
+
add_15
126
+
) # <function create_adder.<locals>.adder at 0x7f8aceb8a950> It return the function and locals object
127
+
print(
128
+
add_15(10)
129
+
) # It will pass the y=10 value it call the create_adder(15)and call the locals pass the value 10
105
130
106
131
# In the above example, the create_adder function returns adder function.
107
132
108
133
# Example 2:
109
134
135
+
110
136
deflogger(msg):
111
137
deflogmsg():
112
-
print('log:',msg) # print global msg
113
-
returnlogmsg# return local function on it
114
-
loghi=logger('Hi!') # pass the msg='Hi!' in logger function it will print inside a logmsg now logger function called and to print logmsg you need to call again
115
-
print(loghi) # <function logger.<locals>.logmsg at 0x7fb9fc8869e0>
138
+
print("log:", msg) # print global msg
139
+
140
+
returnlogmsg# return local function on it
141
+
142
+
143
+
loghi=logger(
144
+
"Hi!"
145
+
) # pass the msg='Hi!' in logger function it will print inside a logmsg now logger function called and to print logmsg you need to call again
146
+
print(loghi) # <function logger.<locals>.logmsg at 0x7fb9fc8869e0>
116
147
loghi()
117
148
118
149
# Example 3:
119
150
151
+
120
152
deftags(tag):
121
153
defwraptext(msg):
122
-
print('<{0}>{1}</{0}>'.format(tag,msg))
154
+
print("<{0}>{1}</{0}>".format(tag, msg))
155
+
123
156
returnwraptext
124
-
printh1=tags('h1') # It call the function pass the tag and it return local function you need to pass a value for locals
125
-
printh1('Hello') # you can pass various msg on it
126
157
127
-
# ------> Nested Function
158
+
159
+
printh1=tags(
160
+
"h1"
161
+
) # It call the function pass the tag and it return local function you need to pass a value for locals
0 commit comments