pep 336: Make None Callable

Bengt Richter bokr at oz.net
Wed Nov 3 23:53:21 EST 2004


On Wed, 03 Nov 2004 20:59:43 -0700, The Eternal Squire <eternalsquire at comcast.net> wrote:

>PEP: 336
>Title: Make None Callable
>Version: $Revision: 1.1 $
>Last-Modified: $Date: 2004/11/03 16:58:30 $
>Author: Andrew McClelland
>Status: Draft
>Type: Standards Track
>Content-Type: text/plain
>Created: 28-Oct-2004
>Post-History:
>
>
>Abstract
>
>     None should be a callable object that when called with any
>     arguments has no side effect and returns None.
>
>
>Motivation
>
>     To allow a programming style for selectable actions that is more
>     in accordance with the minimalistic functional programming goals
>     of the Python language.
IMO in the context of selectable actions, the noop should be a noop function,
not None, e.g.,

    def Noop(*args, **kwargs): pass

>
>
>Rationale
>
>     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
      IMO (3) using a global def Noop should take care of it.

>     arguments similar to other functions in the table.
>
>     The semantics would be effectively,
>
>         class None:
>
>             def __call__(self, *args):
                                      ^-- ,**kw # otherwise it might bomb ;-)
>                 pass
>
>
>How To Use
>
>     Before, checking function table entry against None:
>
>         class Select:
>
>             def a(self, input):
>                 print 'a'
>
>             def b(self, input):
>                 print 'b'
>
>             def c(self, input);
>                 print 'c'
>
>             def __call__(self, input):
>                 function = { 1 : self.a,
>                          2 : self.b,
>                          3 : self.c
                         }.get(input, Noop)
                  return function(input)

#XXX>                        }.get(input, None)
#XXX>                 if function:  return function(input)
>
>     Before, using a local no effect method:
>
>         class Select:
>
>             def a(self, input):
>                 print 'a'
>
>             def b(self, input):
>                 print 'b'
>
>             def c(self, input);
>                 print 'c'
>
#XXX>             def nop(self, input):
#XXX>                 pass
>
>             def __call__(self, input):
>                 return { 1 : self.a,
>                      2 : self.b,
>                      3 : self.c
                         }.get(input, Noop)(input)

#XXX>                        }.get(input, self.nop)(input)
>
>     After:
>
>         class Select:
>
>             def a(self, input):
>                 print 'a'
>
>             def b(self, input):
>                 print 'b'
>
>             def c(self, input);
>                 print 'c'
>
>             def __call__(self, input):
>                 return { 1 : self.a,
>                      2 : self.b,
>                      3 : self.c
                         }.get(input, Noop)(input)

#XXX>                        }.get(input, None)(input)
>
>
>References
>
>     [1] Python Reference Manual, Section 3.2,
>         http://docs.python.org/ref/ref.html
>
>
>Copyright
>
>     This document has been placed in the public domain.
>
>
>
>Local Variables:
>mode: indented-text
>indent-tabs-mode: nil
>sentence-end-double-space: t
>fill-column: 70
>End:

I'd rather not complicate the meaning of None, I think ;-)

Regards,
Bengt Richter



More information about the Python-list mailing list