[New-bugs-announce] [issue27858] Add identity function to functools

Jáchym Barvínek report at bugs.python.org
Thu Aug 25 07:42:15 EDT 2016


New submission from Jáchym Barvínek:

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.

----------
components: Library (Lib)
messages: 273643
nosy: Jáchym Barvínek
priority: normal
severity: normal
status: open
title: Add identity function to functools
type: enhancement
versions: Python 3.6

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue27858>
_______________________________________


More information about the New-bugs-announce mailing list