A suitable 'do nothing' lambda expression in python?

后端 未结 5 1781
遥遥无期
遥遥无期 2020-12-25 09:05

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

def do_nothing(*args):
    pass

B

5条回答
  •  我在风中等你
    2020-12-25 09:47

    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 on the lambda.) Don't forget about **kwargs, if it matters to you. Functions in Python without an explicit return 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.
    

提交回复
热议问题