I’m revisiting some scheme excercises in python (if that makes sense) to find out what python can do in terms of FP. My problem concerns lambda in python :
Can i define a general function in python with an operator as one of the arguments?
Think this :
def f (op,x,y):
#return some lambda function that combines x and y in the appropriate way
#i.e if op is +, then return x+y, if op is -, then return x-y etc
#Edit : added usage
#this should be called like this:
f(+, 1,2) #should return 3
I know this is possible in scheme, but is there something equivalent in python? I’ve gotten the impression that lambda in python is just a shorter way of defining a method, and I’ve not found any way to define a general combiner function in python.
I can see some points in your question, lets go through them in order:
1. Can I pass a function as a parameter to someone?
Yes:
2. What about operators then?
Unlike scheme, Python operators are not functions so you can’t pass them directly as parameters. You can either create the wrapper functions yourself or you can import the operator module from the standard library.
Operators not being real functions is a little sad in most cases but at least Python gives us chained comparisons like
10 <= x < 100in exchange…3. So what is the difference between Python and Scheme then?
In the general sense, functions in Python are as powerful as functions in Scheme, however there are some things to note:
The lambda keyword is limited
You can only have a single expression as the function body
Since there are a bunch of things in Python that are statements and not expressions (assignments, the 2.x
print, …), you often need to fall back to named functions instead.There are closures
But mutating variables in them is a pain
This doesn’t work. It tries to bind a new n for the inner function instead of using the outer one
new 3.x nonlocal keyword
workaround w/ mutable objects
Objects instead of closures. Uses the magic
__call__method to pretend its a function