A suitable 'do nothing' lambda expression in python?

后端 未结 5 1733
遥遥无期
遥遥无期 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 10:06

    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.

提交回复
热议问题