Following system colour scheme Selected dark colour scheme Selected light colour scheme

Python Enhancement Proposals

PEP 559 – Built-in noop()

Author:
Barry Warsaw <barry at Python.org>
Status:
Rejected
Type:
Standards Track
Created:
08-Sep-2017
Python-Version:
3.7
Post-History:
09-Sep-2017
Resolution:
Python-Dev message

Table of Contents

Abstract

This PEP proposes adding a new built-in function callednoop()which does nothing but returnNone.

Rationale

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 forPEP 553,where you could set the breakpoint environment variable to the following in order to effectively disable it:

$ setenv PYTHONBREAKPOINT=noop

Implementation

The Python equivalent of thenoop()function is exactly:

defnoop(*args,**kws):
returnNone

The C built-in implementation is available as a pull request[1].

Rejected alternatives

noop()returns something

YAGNI.

This is rejected because it complicates the semantics. For example, if you always return both*argsand**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 returnNonesince 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 return7or((7,),{})?And so on.

The author claims that you won’t ever need the return value ofnoop()so it will always returnNone.

Coghlan’s Dialogs (edited for formatting):

My counterargument to this would bemap(noop,iterable), sorted(iterable,key=noop),etc. (filter,max,and minall accept callables that accept a single argument, as do many of the itertools operations).

Makingnoop()a useful default function in those cases just needs the definition to be:

defnoop(*args,**kwds):
returnargs[0]ifargselseNone

The counterargument to the counterargument is that usingNone 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.

References


Source:https://github / Python /peps/blob/main/peps/pep-0559.rst

Last modified:2023-09-09 17:39:29 GMT