Is there a builtin identity function in python?

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

Leave a Comment