PEP: 559 Title: Built-in noop() Author: Barry Warsaw <[email protected]> Status: Rejected Type: Standards Track Content-Type: text/x-rst Created: 08-Sep-2017 Python-Version: 3.7 Post-History: 09-Sep-2017 Resolution: https://mail.python.org/pipermail/python-dev/2017-September/149438.html
This PEP proposes adding a new built-in function called noop()
which does
nothing but return None
.
It is trivial to implement a no-op function in Python. It's so easy in fact that many people do it many times over and over again. It would be useful in many cases to have a common built-in function that does nothing.
One use case would be for PEP 553, where you could set the breakpoint environment variable to the following in order to effectively disable it:
$ setenv PYTHONBREAKPOINT=noop
The Python equivalent of the noop()
function is exactly:
def noop(*args, **kws): return None
The C built-in implementation is available as a pull request [1].
YAGNI.
This is rejected because it complicates the semantics. For example, if you
always return both *args
and **kws
, what do you return when none of
those are given? Returning a tuple of ((), {})
is kind of ugly, but
provides consistency. But you might also want to just return None
since
that's also conceptually what the function was passed.
Or, what if you pass in exactly one positional argument, e.g. noop(7)
. Do
you return 7
or ((7,), {})
? And so on.
The author claims that you won't ever need the return value of noop()
so
it will always return None
.
Coghlan's Dialogs (edited for formatting):
My counterargument to this would be
map(noop, iterable)
,sorted(iterable, key=noop)
, etc. (filter
,max
, andmin
all accept callables that accept a single argument, as do many of the itertools operations).Making
noop()
a useful default function in those cases just needs the definition to be:def noop(*args, **kwds): return args[0] if args else NoneThe counterargument to the counterargument is that using
None
as the default in all these cases is going to be faster, since it lets the algorithm skip the callback entirely, rather than calling it and having it do nothing useful.
[1] | python/cpython#3480 |
This document has been placed in the public domain.