Built-In Identity Function in Python

Built-In Identity Function in Python

  1. Use Lambda Functions to Define Identity Functions in Python
  2. Conclusion

The identity function is simply a function that returns its argument. When we define an identity function and assign a value, it returns that value.

While a built-in identity function would mean faster code rendering, Python does not have a built-in identity function because it is quite a trivial function. The if statement in place of the identity function is widely accepted.

Another argument against a built-in identity function is that different structures are required for different tasks. Using a uniform identity function for varying tasks is difficult because it will require constant adjustments.

So since a built-in function would still need to undergo adjustments by the individual users, it was recommended that users find a way to define the identity functions that suit their needs. This opened the door for the lambda function.

Use Lambda Functions to Define Identity Functions in Python

Lambda functions are known as anonymous functions because they are defined without names. We can use the Python function to accomplish varying tasks.

We mentioned earlier that identity functions return the argument assigned to them. We can see this in action when we open the terminal and input this function:

(lambda x: x)(2)

The terminal should return the value 2:

returns argument

The lambda identity function also takes in mathematical calculations and returns the result.

Type this inside the terminal:

(lambda x: x + 5)(2)

This would return the result 7.

lambda for calculations

Then lastly, we will assign multiple arguments to the lambda identity function and return the same.

Input this snippet of code in the terminal:

full_name = lambda first, last: f'{first.title()} {last.title()}'
full_name('James', 'Bond')

The resulting return will be James Bond.

multiple arguments


A built-in identity function was initially in the works, but the project was taken off the shelf as the downsides became apparent. But this gives developers the freedom to come up with their unique solutions.

Oluwafisayo Oluwatayo avatar Oluwafisayo Oluwatayo avatar

Fisayo is a tech expert and enthusiast who loves to solve problems, seek new challenges and aim to spread the knowledge of what she has learned across the globe.


Related Article - Python Function

  • Exit a Function in Python
  • Optional Arguments in Python
  • Fit a Step Function in Python
  • Arguments in the main() Function in Python
  • Python Functools Partial Function