-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfunctions_overview.py
90 lines (66 loc) · 1.77 KB
/
functions_overview.py
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
# simple example of a function
def hello():
'''
just prints hello
:return: nothing
'''
print("hello")
# Functions are just normal objects, and as such they can be inspected
type(hello)
funcs = dir(hello)
# higher-order functions
fruits = ['fig', 'cherry', 'apple', 'blueberry']
sorted(fruits, key=len)
def firstletter(word):
return word[0]
sorted(fruits, key=firstletter)
a = map(lambda x: 2*x, range(6))
type(a)
dir(a)
for x in a:
print(x)
# Lambda Expressions
# example of a simple anonymous function in python
add2 = lambda x: x + 2
print(add2(2))
#
# another way of generating functions with a function
# notice this will create a closure and bind the lambda around a which is
# scoped and bound in the outer function before the lambda is constructed.
#
def create_adder(a):
return lambda b: a + b
add4 = create_adder(4)
add3 = create_adder(3)
print(add4(4))
print(add3(4))
# old way of creating closures without lambdas
def create_adder_old_way(a):
def adder(b):
return a + b
return adder
add5 = create_adder_old_way(5)
print(add5(6))
# example of function that creates functions
def create_greeter():
"""
Example of creating a function that creates function with
inner(scoped) function definitions
"""
def greeter(x):
print(x)
return greeter
a = create_greeter()
b = create_greeter()
a('a is called')
b('b is called')
# Gotcha about closures - Python binds variables in closures by "name" not by "value"
# which causes the following code not to behave as intended
adders = []
for x in range(5):
adders.append(lambda y: x + y)
alist = [adder(10) for adder in adders]
blist = [adder(20) for adder in adders]
def make_adder(x):
return lambda y: x + y
# Exercise: implement the higher-order function map