Is there a builtin identity function in python?

后端 未结 8 1872
情书的邮戳
情书的邮戳 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:15

    Doing some more research, there is none, a feature was asked in issue 1673203 And from Raymond Hettinger said there won't be:

    Better to let people write their own trivial pass-throughs and think about the signature and time costs.

    So a better way to do it is actually (a lambda avoids naming the function):

    _ = lambda *args: args
    
    • advantage: takes any number of parameters
    • disadvantage: the result is a boxed version of the parameters

    OR

    _ = lambda x: x
    
    • advantage: doesn't change the type of the parameter
    • disadvantage: takes exactly 1 positional parameter

提交回复
热议问题