-
A function is an object.
1
square
=
lambda
x: x
*
2
-
Functions can be returned as objects
1234
def
build_taxer(rate):
def
taxer(amount):
return
amount
*
(
float
(rate)
/
100
)
return
taxer
-
filter and lambda
1
filter
(
lambda
x: x
%
2
=
=
0
,
range
(
10
))
-
list comprehensions can replace map and filter, but compute the whole list is slow
1
[i
*
2
for
i
in
range
(
10
)
if
i
%
2
=
=
0
]
-
list is slow actually we only need to compute part of the list
12
def
is_prime(n):
return
not
any
(n
%
k
=
=
0
for
k
in
xrange
(
2
,n))
- in contrast to any(seq), all(seq) returns true if all elements of the sequence are true
Hide Comments