Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

function that returns a function python

# Higher order function - function that take another function as parameter
# Function that returns a function
def mult_by(num):
    return lambda x: x * num
print("3 * 5 =", (mult_by(3)(5)))   #(5) is the parameter for the lambda fxn returned by mult_by()
# 3 * 5 = 15

# Pass a function to a function
def mult_list(list, func):
    for x in list:
        print(func(x), end = ' ')
mult_by_4 = mult_by(4)
mult_list(list(range(0, 5)), mult_by_4)
# 0 4 8 12 16 
 
PREVIOUS NEXT
Tagged: #function #returns #function #python
ADD COMMENT
Topic
Name
3+9 =