Is there a builtin identity function in python?

后端 未结 8 1869
情书的邮戳
情书的邮戳 2020-11-28 07:37

I\'d like to point to a function that does nothing:

def identity(*args)
    return args

my use case is something like this

         


        
8条回答
  •  失恋的感觉
    2020-11-28 08:34

    No, there isn't.

    Note that your identity:

    1. is equivalent to lambda *args: args
    2. Will box its args - i.e.

      In [6]: id = lambda *args: args
      
      In [7]: id(3)
      Out[7]: (3,)
      

    So, you may want to use lambda arg: arg if you want a true identity function.

    NB: This example will shadow the built-in id function (which you will probably never use).

提交回复
热议问题