This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Add identity function to functools
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.6
process
Status: closed Resolution: duplicate
Dependencies: Superseder: add identity function
View: 1673203
Assigned To: Nosy List: Jáchym Barvínek, r.david.murray, rhettinger, serhiy.storchaka, steven.daprano
Priority: normal Keywords: patch

Created on 2016-08-25 11:42 by Jáchym Barvínek, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
identity.patch steven.daprano, 2016-08-25 12:10 review
Messages (7)
msg273643 - (view) Author: Jáchym Barvínek (Jáchym Barvínek) Date: 2016-08-25 11:42
An identity function is sometimes useful in functional-style programming as a dummy or default value.

For example, we can sometimes see a pattern like this (e.g. in itertools.groupby):

def f(params, key=None):
  if key is None:
    key = lambda x: x
  ...


However, if we had a canonical itentity function:

def identity(x):
  return x

we could instead write:

def f(params, key=identity):
  ...

and the intended use of the function f and it's functioning would be more obvious simply from it's signature, while also saving a little code. 

As zen of Python says: Explicit is better than implicit.

Of course, we can now write:

def f(params, key=lambda x: x):
  ...

but the reason why is not used is probably that it feels a bit awkward to more people than just me.
msg273644 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2016-08-25 11:57
I had a work mate make this exact same point literally yesterday. He asked me if Python had an identity function, and when I suggested just using `lambda x: x` he grouched that this wasn't sufficiently obvious enough as "identity" is self-explanatory.

(Perhaps for English speakers with a background in mathematics or functional programming.)

I'm not really convinced. Its easier to just write a lambda than to `from functools import identity`, and despite what my workmate thinks, "identity" is not self-evident. Consider that identity could also be a function that returns a person's identifying information. Think about the English usage of "identity theft" and "secret identity", rather than the mathematical meaning.

So I'm at best +0 on this suggestion.
msg273645 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2016-08-25 12:10
Just in case anyone else thinks this is a good idea, here's a patch.
msg273647 - (view) Author: Jáchym Barvínek (Jáchym Barvínek) Date: 2016-08-25 12:21
Java offers an identity function:
https://docs.oracle.com/javase/8/docs/api/java/util/function/Function.html

2016-08-25 14:10 GMT+02:00 Steven D'Aprano <report@bugs.python.org>:

>
> Steven D'Aprano added the comment:
>
> Just in case anyone else thinks this is a good idea, here's a patch.
>
> ----------
> keywords: +patch
> Added file: https://bugs.python.org/file44218/identity.patch
>
> _______________________________________
> Python tracker <report@bugs.python.org>
> <https://bugs.python.org/issue27858>
> _______________________________________
>
msg273648 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2016-08-25 12:29
I agree with Steven, I don't think this is really worth it, but I wouldn't vote -1.  I suspect it won't get added, though, since a function I do want ('first') got rejected :)
msg273663 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2016-08-25 16:19
I believe this has been previously discussed and declined.
msg273667 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-08-25 16:31
See issue1673203, issue11011.
History
Date User Action Args
2022-04-11 14:58:35adminsetgithub: 72045
2016-08-27 11:17:23serhiy.storchakasetsuperseder: add identity function
resolution: rejected -> duplicate
stage: resolved
2016-08-26 03:24:01rhettingersetstatus: open -> closed
resolution: rejected
2016-08-25 16:31:21serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg273667
2016-08-25 16:19:27rhettingersetnosy: + rhettinger
messages: + msg273663
2016-08-25 12:29:05r.david.murraysetnosy: + r.david.murray
messages: + msg273648
2016-08-25 12:21:45Jáchym Barvíneksetmessages: + msg273647
2016-08-25 12:10:38steven.dapranosetfiles: + identity.patch
keywords: + patch
messages: + msg273645
2016-08-25 11:57:34steven.dapranosetnosy: + steven.daprano
messages: + msg273644
2016-08-25 11:42:15Jáchym Barvínekcreate