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?
do_nothingfunction, but it's not clear why you need to use alambdafor it.lambdapurely 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.None, even though it isn't callable. To wit:filter(None, alist)do_nothingin one place and then passdo_nothingwhenever 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.mock.Mock()can be your dummy callable, accepting any parameters. But it returns another Mock and it may result in weird bugs. Changing it tomock.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.