Nowadays, I am starting to learn haskell, and while I do it, I try to implement some of the ideas I have learned from it in Python. But, I found this one challenging. You can write a function in Haskell, that takes another function as argument, and returns the same function with it’s arguments’ order flipped. Can one do similiar thing in Python? For example,
def divide(a,b):
return a / b
new_divide = flip(divide)
# new_divide is now a function that returns second argument divided by first argument
Can you possibly do this in Python?
You can create a closure in Python using nested function definitions. This lets you create a new function that reverses the argument order and then calls the original function: