2004-11-03 11:58:30 -05:00
|
|
|
PEP: 336
|
|
|
|
Title: Make None Callable
|
|
|
|
Version: $Revision$
|
2017-01-10 14:30:39 -05:00
|
|
|
Last-Modified: $Date$
|
2008-12-29 22:48:55 -05:00
|
|
|
Author: Andrew McClelland <eternalsquire@comcast.net>
|
2005-06-17 14:27:15 -04:00
|
|
|
Status: Rejected
|
2004-11-03 11:58:30 -05:00
|
|
|
Type: Standards Track
|
2017-01-10 14:30:39 -05:00
|
|
|
Content-Type: text/x-rst
|
2004-11-03 11:58:30 -05:00
|
|
|
Created: 28-Oct-2004
|
2017-01-10 14:30:39 -05:00
|
|
|
Post-History:
|
2004-11-03 11:58:30 -05:00
|
|
|
|
|
|
|
|
|
|
|
Abstract
|
2017-01-10 14:30:39 -05:00
|
|
|
========
|
|
|
|
|
|
|
|
``None`` should be a callable object that when called with any
|
|
|
|
arguments has no side effect and returns ``None``.
|
2004-11-03 11:58:30 -05:00
|
|
|
|
|
|
|
|
2005-06-17 13:43:00 -04:00
|
|
|
BDFL Pronouncement
|
2017-01-10 14:30:39 -05:00
|
|
|
==================
|
2005-06-17 13:43:00 -04:00
|
|
|
|
2017-01-10 14:30:39 -05:00
|
|
|
This PEP is rejected. It is considered a feature that ``None`` raises
|
|
|
|
an error when called. The proposal falls short in tests for
|
2019-07-03 14:20:45 -04:00
|
|
|
obviousness, clarity, explicitness, and necessity. The provided Switch
|
2017-01-10 14:30:39 -05:00
|
|
|
example is nice but easily handled by a simple lambda definition.
|
2023-08-01 19:03:32 -04:00
|
|
|
See python-dev discussion on 17 June 2005 [1]_.
|
2005-06-17 13:43:00 -04:00
|
|
|
|
2004-11-03 11:58:30 -05:00
|
|
|
|
|
|
|
Motivation
|
2017-01-10 14:30:39 -05:00
|
|
|
==========
|
2004-11-03 11:58:30 -05:00
|
|
|
|
2017-01-10 14:30:39 -05:00
|
|
|
To allow a programming style for selectable actions that is more
|
|
|
|
in accordance with the minimalistic functional programming goals
|
|
|
|
of the Python language.
|
2004-11-03 11:58:30 -05:00
|
|
|
|
|
|
|
|
|
|
|
Rationale
|
2017-01-10 14:30:39 -05:00
|
|
|
=========
|
2004-11-03 11:58:30 -05:00
|
|
|
|
2017-01-10 14:30:39 -05:00
|
|
|
Allow the use of ``None`` in method tables as a universal no effect
|
|
|
|
rather than either (1) checking a method table entry against ``None``
|
|
|
|
before calling, or (2) writing a local no effect method with
|
|
|
|
arguments similar to other functions in the table.
|
2004-11-03 11:58:30 -05:00
|
|
|
|
2017-01-10 14:30:39 -05:00
|
|
|
The semantics would be effectively::
|
2004-11-03 11:58:30 -05:00
|
|
|
|
2017-01-10 14:30:39 -05:00
|
|
|
class None:
|
2004-11-03 11:58:30 -05:00
|
|
|
|
2017-01-10 14:30:39 -05:00
|
|
|
def __call__(self, *args):
|
|
|
|
pass
|
2004-11-03 11:58:30 -05:00
|
|
|
|
|
|
|
|
|
|
|
How To Use
|
2017-01-10 14:30:39 -05:00
|
|
|
==========
|
2004-11-03 11:58:30 -05:00
|
|
|
|
2017-01-10 14:30:39 -05:00
|
|
|
Before, checking function table entry against ``None``::
|
2004-11-03 11:58:30 -05:00
|
|
|
|
2017-01-10 14:30:39 -05:00
|
|
|
class Select:
|
2004-11-03 11:58:30 -05:00
|
|
|
|
2017-01-10 14:30:39 -05:00
|
|
|
def a(self, input):
|
|
|
|
print 'a'
|
2004-11-03 11:58:30 -05:00
|
|
|
|
2017-01-10 14:30:39 -05:00
|
|
|
def b(self, input):
|
|
|
|
print 'b'
|
2004-11-03 11:58:30 -05:00
|
|
|
|
2019-04-07 12:19:10 -04:00
|
|
|
def c(self, input):
|
2017-01-10 14:30:39 -05:00
|
|
|
print 'c'
|
2004-11-03 11:58:30 -05:00
|
|
|
|
2017-01-10 14:30:39 -05:00
|
|
|
def __call__(self, input):
|
|
|
|
function = { 1 : self.a,
|
|
|
|
2 : self.b,
|
|
|
|
3 : self.c
|
|
|
|
}.get(input, None)
|
|
|
|
if function: return function(input)
|
2004-11-03 11:58:30 -05:00
|
|
|
|
2017-01-10 14:30:39 -05:00
|
|
|
Before, using a local no effect method::
|
2004-11-03 11:58:30 -05:00
|
|
|
|
2017-01-10 14:30:39 -05:00
|
|
|
class Select:
|
2004-11-03 11:58:30 -05:00
|
|
|
|
2017-01-10 14:30:39 -05:00
|
|
|
def a(self, input):
|
|
|
|
print 'a'
|
2004-11-03 11:58:30 -05:00
|
|
|
|
2017-01-10 14:30:39 -05:00
|
|
|
def b(self, input):
|
|
|
|
print 'b'
|
2004-11-03 11:58:30 -05:00
|
|
|
|
2019-04-07 12:19:10 -04:00
|
|
|
def c(self, input):
|
2017-01-10 14:30:39 -05:00
|
|
|
print 'c'
|
2004-11-03 11:58:30 -05:00
|
|
|
|
2017-01-10 14:30:39 -05:00
|
|
|
def nop(self, input):
|
|
|
|
pass
|
2004-11-03 11:58:30 -05:00
|
|
|
|
2017-01-10 14:30:39 -05:00
|
|
|
def __call__(self, input):
|
|
|
|
return { 1 : self.a,
|
|
|
|
2 : self.b,
|
|
|
|
3 : self.c
|
|
|
|
}.get(input, self.nop)(input)
|
2004-11-03 11:58:30 -05:00
|
|
|
|
2017-01-10 14:30:39 -05:00
|
|
|
After::
|
2004-11-03 11:58:30 -05:00
|
|
|
|
2017-01-10 14:30:39 -05:00
|
|
|
class Select:
|
2004-11-03 11:58:30 -05:00
|
|
|
|
2017-01-10 14:30:39 -05:00
|
|
|
def a(self, input):
|
|
|
|
print 'a'
|
2004-11-03 11:58:30 -05:00
|
|
|
|
2017-01-10 14:30:39 -05:00
|
|
|
def b(self, input):
|
|
|
|
print 'b'
|
2004-11-03 11:58:30 -05:00
|
|
|
|
2019-04-07 12:19:10 -04:00
|
|
|
def c(self, input):
|
2017-01-10 14:30:39 -05:00
|
|
|
print 'c'
|
2004-11-03 11:58:30 -05:00
|
|
|
|
2017-01-10 14:30:39 -05:00
|
|
|
def __call__(self, input):
|
|
|
|
return { 1 : self.a,
|
|
|
|
2 : self.b,
|
|
|
|
3 : self.c
|
|
|
|
}.get(input, None)(input)
|
2004-11-03 11:58:30 -05:00
|
|
|
|
|
|
|
|
|
|
|
References
|
2017-01-10 14:30:39 -05:00
|
|
|
==========
|
|
|
|
|
2023-08-01 19:03:32 -04:00
|
|
|
.. [1] Raymond Hettinger, Propose to reject PEP 336 -- Make None Callable
|
2017-01-10 14:30:39 -05:00
|
|
|
https://mail.python.org/pipermail/python-dev/2005-June/054280.html
|
2004-11-03 11:58:30 -05:00
|
|
|
|
|
|
|
|
|
|
|
Copyright
|
2017-01-10 14:30:39 -05:00
|
|
|
=========
|
|
|
|
|
|
|
|
This document has been placed in the public domain.
|