138

I sometimes find myself wanting to make placeholder 'do nothing' lambda expressions, similar to saying:

def do_nothing(*args):
    pass

But the following syntax is illegal since lambda expressions attempt to return whatever is after the colon, and you can't return pass.

do_nothing = lambda *args: pass

So I was wondering, would the following expression be a suitable replacement for the above?

do_nothing = lambda *args: None

Since the do_nothing function above technically returns None, is it okay to make a lambda expression that returns None to use as a placeholder lambda expression? Or is it bad practice?

5
  • It does do the same thing as the do_nothing function, but it's not clear why you need to use a lambda for it. Commented Mar 29, 2014 at 23:47
  • I wanted to use lambda purely for readability of code, since I'm personally not a fan of nested functions and the application I wanted to use the expression in would be a third nested function. I'm also simply just interested in other opinions about using this coding trope. Commented Mar 29, 2014 at 23:50
  • 1
    In idiomatic Python, most places where you would need a do-nothing function accept None, even though it isn't callable. To wit: filter(None, alist) Commented Mar 29, 2014 at 23:51
  • 8
    Since all do-nothing functions do the same nothing, you could just define do_nothing in one place and then pass do_nothing whenever you want to pass a do-nothing. There's no need to redefine it with a new lambda (or full function def) every time you want to use it. Commented Mar 29, 2014 at 23:51
  • mock.Mock() can be your dummy callable, accepting any parameters. But it returns another Mock and it may result in weird bugs. Changing it to mock.Mock(return_value=None) can be still buggy since you can try to get an attribute from it and it will work, returning another Mock. Commented May 19, 2017 at 11:23

6 Answers 6

166

This:

def do_nothing(*args):
    pass

is equivalent to:

lambda *args: None

With some minor differences in that one is a lambda and one isn't. (For example, __name__ will be do_nothing on the function, and <lambda> on the lambda.) Don't forget about **kwargs, if it matters to you. Functions in Python without an explicit return <x> return None. This is here:

A call always returns some value, possibly None, unless it raises an exception.

I've used similar functions as default values, say for example:

def long_running_code(progress_function=lambda percent_complete: None):
    # Report progress via progress_function.
Sign up to request clarification or add additional context in comments.

1 Comment

This also works in ternary conditional expressions: DEBUG = print if DEBUGSW == True else lambda *args: None
76

If you truly want a full do nothing lambda function, make sure to gobble up *args and *kwargs.

noop = lambda *args, **kwargs: None

In all its glorious action

>>> noop = lambda *args, **kwargs: None
>>> noop("yes", duck_size="horse", num_ducks=100)
>>>

Side Note

Since the accepted answer is only using *args, I must point out that you will do yourself a favor for the future by including the **kwargs handling. If you ever try to use the noop somewhere deep away in your code and you forgot that it doesn't take kwargs, it will be quite the Exception to doing nothing:

In [2]: do_nothing('asdf', duck="yes")
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-2-efbd722f297c> in <module>()
----> 1 do_nothing('asdf', duck="yes")

TypeError: <lambda>() got an unexpected keyword argument 'duck'

1 Comment

If you assign the function to a variable, it is much better to do it the normal way: def noop(*args, **kwargs): return None
29

Sometimes lambda functions are used for data transformation, and in that case 'do nothing' means to return the input, i.e.

lambda x: x

To return none you can write

lambda x: None

2 Comments

That's not the same as a do-nothing function that returns None.
I like this thought. It's more like the multiplicative identity than do_nothing. Maybe there's a case for having one of each.
21

This is the simplest lambda expression:

do_nothing = lambda: None

No arguments required and the minimal required return.

2 Comments

This is the cleanest solution
This raises TypeError when arguments, positional or keyword, are passed.
6

In Python 3 you don't even need to define any functions for that. Calling type(None) will return you the NoneType constructor, which you can use for doing nothing: type(None)(). Keep in mind that the NoneType constructor only takes 0 arguments.

In Python 2, though, creating instances of NoneType is impossible, so lambda: None would make the most sense.

Comments

1

Besides this expression

lambda *args: None

there is another graceful expression using Ellipsis:

lambda *args: ...

This can also be useful in case the function has no arguments (callback for example):

lambda: None

lambda: ...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.