None should be a callable object that when called with anyarguments has no side effect and returnsNone.
This PEP is rejected. It is considered a feature thatNone raisesan error when called. The proposal falls short in tests forobviousness, clarity, explicitness, and necessity. The provided Switchexample is nice but easily handled by a simple lambda definition.See python-dev discussion on 17 June 2005[1].
To allow a programming style for selectable actions that is morein accordance with the minimalistic functional programming goalsof the Python language.
Allow the use ofNone in method tables as a universal no effectrather than either (1) checking a method table entry againstNonebefore calling, or (2) writing a local no effect method witharguments similar to other functions in the table.
The semantics would be effectively:
classNone:def__call__(self,*args):pass
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):passdef__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)
This document has been placed in the public domain.
Source:https://github.com/python/peps/blob/main/peps/pep-0336.rst
Last modified:2025-02-01 08:59:27 GMT