7.1 What Makes a Function “Anonymous”?

An anonymous function is a function that does not have a name. Let’s go back to the example function (i.e., the one that I made using def) that was shown at the beginning of this section:

def makeMatrix(nrow, ncol):
  return [(['o'] * ncol) for i in range(nrow)]

We know that the function’s name is makeMatrix - it also takes in two arguments, nrow and ncol.

However, anonymous functions do not have names - furthermore, they are not defined using def, but by using the keyword lambda (from lambda calculus). Because, of this, an anonymous function is also called a lambda function.

7.1.1 Syntax of an anonymous function

Here is makeMatrix() as an anonymous function:

makeMatrix = lambda nrow, ncol : [(['o'] * ncol) for i in range(nrow)]

Unlike def, you do not indent anything. Rather, after you declare your anonymous function with lambda, what comes after (i.e., nrow and ncol) are your function’s arguments.

One then leaves a colon after their arguments; what follows after the colon is the function’s definition!

7.1.1.1 Why does it look like the anonymous function has a name?

Rest assured, it doesn’t.

makeMatrix = lambda nrow, ncol : [(['o'] * ncol) for i in range(nrow)]

What lambda does is it returns a function object that is stored in the variable makeMatrix.

In other words, makeMatrix is just a variable to store the contents of our anonymous function, but it is not the function itself (unlike the case with def).

7.1.2 When do I use anonymous functions?

Ideally, you shouldn’t use them too often: reason being that they make things harder to read. May be it more concise in some cases? Perhaps, but readability and maintainability is always more important than conciseness or “cool” code!

For this reason, I suggest using anonymous functions if any one or more of these three bullet points apply to you or your team:

  1. The operation you are trying to do is very simple

    Suppose that I had a list of names names that I wanted to sort by length (in decreasing order). While I can define a new function using def and proceed to use the sorted() function thereafter, it really doesn’t make that much sense to do that, especially since it’s something that can be very easily done:

    names = ["Kevin", "Kenneth", "Janice", "Alice", "Emma"]
    
    nameSorted = sorted(names, key = lambda x : len(x), reverse = True)

    Hence, I can use an anonymous function to avoid writing too much code!

  2. If you are working in a team and everybody knows what the anonymous function does

    Like I mentioned earlier, anonymous functions make things harder to read; also, if you remember what I said about how programming is almost always never a solo effort, you really do not want to confuse your fellow developers or give them a rough time when they try to modify your code!

    So, I would suggest leaving a comment explaining the purpose of the anonymous function (and your justification(s) for using an anonymous function) if you insist on doing this in a team setting.

  3. When using an anonymous function makes more sense than doing anything else

    Granted - I can’t currently think of any, but if you can and are able to justify your decisions, then by all means, go ahead!