Created for
def greet(name):
print("Hello, {}".format(name))
#a function can be assigned to variable:
foo = greet
greet("Maria")
foo("Pesho")
#OUTPUT
# Hello, Maria
# Hello, Pesho
def greet(name):
print("Hello, {}".format(name))
# a function can be passed as argument to another function
def wrapper(f, n):
f(n)
wrapper(greet, "Alex")
#OUTPUT
# Hello, Alex
#a function can be returned as value from another function
def greet_wrapper(name):
def wrapper():
print("Hello, {}".format(name))
return wrapper
greet = greet_wrapper("Viktor")
greet()
decorated = decorator(decorated)
def stars_decorator(f):
def wrapper():
print("*" * 50)
f()
print("*" * 50)
return wrapper
def greet():
print("Howdy World!")
# let's decorate greet:
greet = stars_decorator(greet)
# and use it:
greet()
@decorator_name
) for calling higher-order functions.
#define the decorator:
def decorator():
pass
# define a function to be decorated:
@decorator
def decorated():
pass
# just call the decorated function:
decorated()
#define the decorator:
def stars_decorator(f):
def wrapper():
print("*" * 50)
f()
print("*" * 50)
return wrapper
# let's decorate greet:
@stars_decorator
def greet():
print("Howdy World!")
# and use it:
greet()
What about if we want the decorated function to take some arguments?
def stars_decorator(f):
def wrapper(n):
print("*" * 50)
f(n)
print("*" * 50)
return wrapper
def greet(name):
print("Howdy {}!".format(name))
# let's decorate greet:
greet = stars_decorator(greet)
# and use it:
greet("Pesho")
def stars_decorator(f):
def wrapper(n):
print("*" * 50)
f(n)
print("*" * 50)
return wrapper
# let's decorate greet:
@stars_decorator
def greet(name):
print("Howdy {}!".format(name))
# and use it:
greet("Pesho")
def
and return
.Lamda functions are defined by:
lambda parameter_list: expression
which is equivalent to
def some_func(arguments):
return expression
but without a name
Note, that in bellow examples, lambda functions are assigned to variables, just to demonstrate the lambda syntax in simplest context. This is not Pythonic - lambdas should be used as anonymous functions. And we will see real lambda usage in next slides.
l = lambda x,y:x+y
print( l(2,3) )
maxnum = lambda x,y: x if x>y else y
print( maxnum(30,40) )
filter()
Functionfilter()
Function
filter(function, iterable)
def is_even(x):
return True if x!=0 and x%2==0 else False
even_numbers = filter(is_even, range(10))
print( list(even_numbers) )
# [2, 4, 6, 8]
even_numbers = filter(lambda x: x%2==0 if x!=0 else False, range(10))
print( list(even_numbers) )
names = ["Ivan", "", "Alex", "", "Maria", "Angel", ""]
not_empty_names = filter(lambda s: s, names)
print( list(not_empty_names) )
names = ["Ivan", "Alex", "Maria", "Angel", ""]
filtered_names = filter(lambda s: s and s[0]=="A", names)
print( list(filtered_names) )
map()
Functionmap()
Function
map(function, iterable, ...)
map numbers in a list to a list of squared numbers:
numbers = [1,2,3,4,5]
sqr_numbers = map(lambda x:x**2, numbers)
print( list(sqr_numbers) )
#[1, 4, 9, 16, 25]
letters = map(chr, range(1040, 1072))
print( list(letters) )
#['А', 'Б', 'В', 'Г', 'Д', 'Е', 'Ж', 'З', 'И', 'Й', 'К', 'Л', 'М', 'Н', 'О', 'П', 'Р', 'С', 'Т', 'У', 'Ф', 'Х', 'Ц', 'Ч', 'Ш', 'Щ', 'Ъ', 'Ы', 'Ь', 'Э', 'Ю', 'Я']
l1 = [1,2,3]
l2 = [1,2,3]
l_sum = map(lambda a,b: a+b, l1, l2)
print( list(l_sum) )
# [2, 4, 6]
reduce()
Functionreduce()
Functionreduce()
in Python2 and Python3functools
module!from functools import reduce
reduce(function, iterable)
reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates ((((1+2)+3)+4)+5).
from functools import reduce
res = reduce(lambda a,b: a+b, range(11))
print(res)
#55
Sum variable number of lists items positionally
l1 = [1,2,3]
l2 = [1,2,3]
l3 = [1,2,3]
l = map(lambda *t: reduce(lambda a,b: a+b, t) , l1, l2,l3)
print( list(l) )
# [3, 6, 9]
These slides are based on
customised version of
framework