passing a function as an argument in python


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):
        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).

Asked By: Salvador Dali



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:
Why use lambda functions?

Answered By: aIKid

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 )

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.

Answered By: Max Noel
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.