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.
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:35 | admin | set | github: 72045 |
| 2016年08月27日 11:17:23 | serhiy.storchaka | set | superseder: add identity function resolution: rejected -> duplicate stage: resolved |
| 2016年08月26日 03:24:01 | rhettinger | set | status: open -> closed resolution: rejected |
| 2016年08月25日 16:31:21 | serhiy.storchaka | set | nosy:
+ serhiy.storchaka messages: + msg273667 |
| 2016年08月25日 16:19:27 | rhettinger | set | nosy:
+ rhettinger messages: + msg273663 |
| 2016年08月25日 12:29:05 | r.david.murray | set | nosy:
+ r.david.murray messages: + msg273648 |
| 2016年08月25日 12:21:45 | Jáchym Barvínek | set | messages: + msg273647 |
| 2016年08月25日 12:10:38 | steven.daprano | set | files:
+ identity.patch keywords: + patch messages: + msg273645 |
| 2016年08月25日 11:57:34 | steven.daprano | set | nosy:
+ steven.daprano messages: + msg273644 |
| 2016年08月25日 11:42:15 | Jáchym Barvínek | create | |