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