Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5316c13

Browse files
paste in relevent folder
1 parent 9c1253f commit 5316c13

File tree

4 files changed

+111
-74
lines changed

4 files changed

+111
-74
lines changed
File renamed without changes.
File renamed without changes.
File renamed without changes.

‎Notes/advance/firstclassfun.py‎

Lines changed: 111 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# First class functions
22

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.
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.
66
# Python supports the concept of First Class functions.
77

88
# Properties of first class functions:
@@ -15,113 +15,150 @@
1515

1616
# Examples illustrating First Class functions in Python
1717

18-
# 1. Functions are objects:
19-
# Python functions are first class objects.
18+
# 1. Functions are objects:
19+
# Python functions are first class objects.
2020

21-
# Python program to illustrate functions
22-
# can be treated as objects
23-
def shout(text):
24-
return text.upper() # Print Uppercase
21+
# Python program to illustrate functions
22+
# can be treated as objects
23+
def shout(text):
24+
return text.upper() # Print Uppercase
2525

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
3234

3335
# In the above program we are assigning function to a variable
3436
# This assignment doesn’t call the function. (yell=shot)
3537
# It takes the function object referenced by shout and creates a second name pointing to it, yell.(yell -->shot)
3638

39+
3740
# Example 2:
3841
def square(x):
39-
return x*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-
def shout(text): # the function was called from greet and it contains values on it
55-
return text.upper()
56-
57-
def whisper(text): # the function was called from greet and it contains values on it
58-
return text.lower()
59-
60-
def greet(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+
return x * 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+
def shout(text): # the function was called from greet and it contains values on it
61+
return text.upper()
62+
63+
64+
def whisper(text): # the function was called from greet and it contains values on it
65+
return text.lower()
66+
67+
68+
def greet(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.
6879

6980
# In the example above, we have created a function greet which takes a function as an argument.
7081

82+
7183
# Example 2:
7284
def square(x):
73-
return x*x
85+
return x * x
86+
7487

7588
def cube(x):
76-
return x*x*x
89+
return x*x*x
7790

78-
def myfunc(func,args):
79-
result=[]
91+
92+
def myfunc(func, args):
93+
result = []
8094
for i in args:
8195
result.append(func(i))
8296
return result
8397

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]
8898

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:
90109
# Because functions are objects we can return a function from another function.
91110

92-
# Python program to illustrate functions
93-
# Functions can return another function
94-
95-
def create_adder(x): # x=15
96-
def adder(y):
97-
print(x) # x=15 value will be passed inside adder function
98-
return x+y
99-
100-
return adder # 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+
def create_adder(x): # x=15
116+
def adder(y):
117+
print(x) # x=15 value will be passed inside adder function
118+
return x + y
119+
120+
return adder # 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
105130

106131
# In the above example, the create_adder function returns adder function.
107132

108133
# Example 2:
109134

135+
110136
def logger(msg):
111137
def logmsg():
112-
print('log:',msg) # print global msg
113-
return logmsg # 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+
return logmsg # 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>
116147
loghi()
117148

118149
# Example 3:
119150

151+
120152
def tags(tag):
121153
def wraptext(msg):
122-
print('<{0}>{1}</{0}>'.format(tag,msg))
154+
print("<{0}>{1}</{0}>".format(tag, msg))
155+
123156
return wraptext
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
126157

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
162+
printh1("Hello") # you can pass various msg on it
163+
164+
# ------> Nested Function

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /