Suppose I want to calculate the following f(f(...f(x)..)
.
Basically many times function of itself.
Currently I am doing the following to achieve this result (and to return all the intermediate steps):
def iterator(function, x, n=4): x = float(x) arr = [] for i in range(n + 1): arr.append(x) x = function(x) return arr def funcM(x): return x / 4 + 12
and then I am passing my function funcM
as an argument:print iterator(funcM, 100, 5)
.
There is nothing wrong with this approach, and calculations are correct.
But is there a way to do the same without defining function funcM
?
May be passing lambda function as an argument to iterator function (Sorry if it does not make sense, I do not really know what lambda functions are).
Because functions are objects we can pass them as arguments to other functions. Functions that can accept other functions as arguments are also called higher-order functions.
Functions are data, and therefore can be passed around just like other values. This means a function can be passed to another function as an argument. This allows the function being called to use the function argument to carry out its action.
By default, arguments may be passed to a Python function either by position or explicitly by keyword.
A lambda function (or more accurately, a lambda expression) is simply a function you can define on-the-spot, right where you need it. For example,
f = lambda x: x * 2
is exactly the same thing as
def f(x): return x * 2
And when I say exactly, I mean it -- they disassemble to the same bytecode. The only difference between the two is that the one defined in the second example has a name.
Lambda expressions become useful because creating one is not a statement, which means that, as others have already answered, you can do
print iterator(lambda x: x / 4 + 12, 100, 5)
to get precisely what you want.
The main difference between lambda expressions and regular functions, however, is that lambdas are more limited. Lambdas can only contain expressions, not statements. An expression is anything you can put on the right side of an =
assignment. (if you want to get more pedantic, Python defines an expression as http://docs.python.org/2/reference/expressions.html )
What this means is a lambda expression can not assign to a variable (in fact, it can't have local variables at all, other than its parameters). It can't print (unless it calls another function that does). It can't have a for loop, a while loop, an if test (other than the ternary operator x if cond else y
), or a try/except block.
If you need to do any of those, just define a regular function. In fact, any time you think you want to use a lambda, think twice. Wouldn't the code be more readable if you used a regular function? Isn't that lambda expression something you'd like to reuse somewhere else in your code?
In the end, always do what leads to the most readable and maintainable code. There is no difference between lambdas and normal functions as far as performance is concerned.
Yes, you can just use lambda expressions. They are made for this.
iterator(lambda x: x/4+12, 100, 5)
Words from the docs:
Lambdas are usually used to create small, anonymous functions. Actually, they are just a syntatic sugar to define functions. The lambda expression above is exactly the same as your function, only without a name.
If you wish to learn more, Here is some good read:
http://www.diveintopython.net/power_of_introspection/lambda_functions.html
Why use lambda functions?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With