37
loading...
This website collects cookies to deliver better user experience
def plusOne(number):
return number+1
def plusOne(number):
return number+1
#renaming the above function
re = plusOne
#calling both the functions
plusOne(10) # 11
re(10) # 11
x = 11
y = x
print(x) # 11
print(y) # 11
def plusOne(number):
return number+1
def plusTwo(number):
return number+2
def plusThree(number):
return number+3
def plusFour(number):
return number+4
# list storing function objects
func_list = [plusOne, plusTwo, plusThree, plusFour]
# executing each function
for func in func_list:
print(func(10))
# It will print out ---> 11, 12, 13, 14
func_list
list which will store the function objects. Now, before I move further, it is imperative to understand that Python does not make a call to the function unless you succeed it with brackets ()
. So, if I only do this:print(plusOne)
# prints out ---> <function plusOne at 0x000001B4FC7FEF70>
func_list
holds the function objects (which are not yet executed). Then, I ran a for loop, accessed each function and then finally executed it with the help of parenthesis and a parameter, 10.def plusOne(number):
return number+1
def plusTwo(number):
return number+2
def listModifier(li, func):
result = []
for element in li:
result.append(func(element))
return result
# executing listModifier
print(listModifier([1,2,3,4], plusOne))
# prints out ---> [2,3,4,5]
plusOne
) as the parameter to another function (listModifier
). Then, I made a call to plusOne
from within the listModifier
function. This would trigger the plusOne
function and will increment each value of list passed and then append it to a result
list. Finally, after the above operation, simply returned result
.plusOne
parameter of listModifier
to plusTwo
like so:print(listModifier([1,2,3,4], plusTwo)) # pretty neat eh?
# prints out ---> [3,4,5,6]