Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it pythonic: naming lambdas

Tags:

python

lambda

I'm beginning to appreciate the value of lambda expressions in python, particularly when it comes to functional programming, map, functions returning functions, etc. However, I've also been naming lambdas within functions because:

  • I need the same functionality several times and don't want to repeat code.
  • The functionality is specific to the function in which it appears; its not needed elsewhere.

When I encounter a situation that meets the above criteria, I've been writing a named lambda expression in order to DRY and narrowly scope functionality. For example, I am writing a function that operates on some numpy arrays, and I need to do some moderately tedious indexing of all the arrays passed to the function (which can easily fit on a single line). I've written a named lambda expression to do the indexing instead of writing a whole other function or copy/pasting the indexing several times throughout the function definition.

def fcn_operating_on_arrays(array0, array1):
    indexer = lambda a0, a1, idx: a0[idx] + a1[idx]
    
    # codecodecode
    
    indexed = indexer(array0, array1, indices)
    
    # codecodecode in which other arrays are created and require `indexer`
    
    return the_answer

Is this an abuse of python's lambdas? Should I just suck it up and define a separate function?

Edits

Probably worth linking function inside function.

like image 251
joshua.r.smith Avatar asked Jul 14 '16 18:07

joshua.r.smith


People also ask

Are lambdas Pythonic?

Best practices for using Python lambdas Perceived as very Pythonic, lambdas are one of the favorite features in Python programming. So much so, that many Python programmers are tempted to use them whenever possible.

How do you name a lambda?

The closest you can get to renaming the lambda function is using an alias, which is a way to name a specific version of a lambda. The actual name of the function though, is set once you create it. If you want to rename it, just create a new function and copy the exact same code into it.

Can lambda be a variable name in Python?

Lambda Function, also referred to as 'Anonymous function' is same as a regular python function but can be defined without a name. While normal functions are defined using the def keyword, anonymous functions are defined using the lambda keyword. However,they are restricted to single line of expression.

Why is it called lambda Python?

The term “Lambda” comes from mathematics, where it's called lambda calculus. In programming, a Lambda expression (or function) is just an anonymous function, i.e., a function with no name. In fact, some Lambda expressions don't even have a function body.


2 Answers

This is not Pythonic and PEP8 discourages it:

Always use a def statement instead of an assignment statement that binds a lambda expression directly to an identifier.

Yes:

def f(x): return 2*x

No:

f = lambda x: 2*x

The first form means that the name of the resulting function object is specifically 'f' instead of the generic '<lambda>'. This is more useful for tracebacks and string representations in general. The use of the assignment statement eliminates the sole benefit a lambda expression can offer over an explicit def statement (i.e. that it can be embedded inside a larger expression)

A rule of thumb for this is to think on its definition: lambdas expressions are anonymous functions. If you name it, it isn't anonymous anymore. :)

like image 81
Delgan Avatar answered Oct 20 '22 19:10

Delgan


I've written a named lambda expression to do the indexing instead of writing a whole other function

Well, you are writing a whole other function. You're just writing it with a lambda expression.

Why not use def? You get nicer stack traces and more syntactical flexibility, and you don't lose anything. It's not like def can't occur inside another function:

def fcn_operating_on_arrays(array0, array1):
    def indexer(a0, a1, idx):
        return a0[idx] + a1[idx]
    ...
like image 38
user2357112 supports Monica Avatar answered Oct 20 '22 18:10

user2357112 supports Monica