Express What, not How.

Rainer Deyke rainerd at eldwood.com
Wed Oct 15 02:15:15 EDT 2003


One of the most compelling arguments for anonymous functions comes from
Smalltalk, where the 'if' statement is implemented as a higher order method.
The equivalent in Python would look something like this:

class true_class:
  def if_true(self, fn): return fn()
  def if_false(self, fn): pass

class false_class:
  def if_true(self, fn): pass
  def if_false(self, fn): return fn()

true = true_class()
false = false_class()

(x == 0).if_true(anonymous_code_block)

Sure, Python already has an 'if' statement.  That does not mean that there
are no other control structures worth implementing as HOFs.

However, I think I can make an even stronger case for anonymous functions
from my own code, which is littered with lambda expressions such as 'lambda:
1', 'lambda: 0', and 'lambda: None'.  Giving those mini functions individual
names would be an atrocity, and even a factory function that returns these
mini functions would only hurt readability.


-- 
Rainer Deyke - rainerd at eldwood.com - http://eldwood.com






More information about the Python-list mailing list