PEP 336 – Make None Callable
- Author:
- Andrew McClelland <eternalsquire at comcast.net>
- Status:
- Rejected
- Type:
- Standards Track
- Created:
- 28-Oct-2004
- Post-History:
Table of Contents
Abstract
None
should be a callable object that when called with any
arguments has no side effect and returnsNone
.
BDFL Pronouncement
This PEP is rejected. It is considered a feature thatNone
raises
an error when called. The proposal falls short in tests for
obviousness, clarity, explicitness, and necessity. The provided Switch
example is nice but easily handled by a simple lambda definition.
See Python -dev discussion on 17 June 2005[1].
Motivation
To allow a programming style for selectable actions that is more in accordance with the minimalistic functional programming goals of the Python language.
Rationale
Allow the use ofNone
in method tables as a universal no effect
rather than either (1) checking a method table entry againstNone
before calling, or (2) writing a local no effect method with
arguments similar to other functions in the table.
The semantics would be effectively:
classNone:
def__call__(self,*args):
pass
How To Use
Before, checking function table entry againstNone
:
classSelect:
defa(self,input):
print'a'
defb(self,input):
print'b'
defc(self,input):
print'c'
def__call__(self,input):
function={1:self.a,
2:self.b,
3:self.c
}.get(input,None)
iffunction:returnfunction(input)
Before, using a local no effect method:
classSelect:
defa(self,input):
print'a'
defb(self,input):
print'b'
defc(self,input):
print'c'
defnop(self,input):
pass
def__call__(self,input):
return{1:self.a,
2:self.b,
3:self.c
}.get(input,self.nop)(input)
After:
classSelect:
defa(self,input):
print'a'
defb(self,input):
print'b'
defc(self,input):
print'c'
def__call__(self,input):
return{1:self.a,
2:self.b,
3:self.c
}.get(input,None)(input)
References
Copyright
This document has been placed in the public domain.
Source:https://github / Python /peps/blob/main/peps/pep-0336.rst
Last modified:2023-09-09 17:39:29 GMT