A friendlier, sugarier lambda -- a proposal for Ruby-like blocks in python

Paul Boddie paul at boddie.org.uk
Sat Oct 14 18:25:00 CEST 2006

Kay Schluehr wrote:
> Spreading tiny function definitions all over the code
> may be finally not such a good idea compared with a few generic methods
> that get just called? OO might run out of fashion these days but Python
> is not Java and Pythons OO is pretty lightweight.

I think you've successfully identified a recent trend in Python
development: the abandonment of fairly transparent object-oriented
techniques in favour of more opaque but supposedly more convenient
hybrid techniques. Unlike Java, Python's first class functions and
methods are already highly useful for callback-based systems - such
systems in Java suffer from interface proliferation and needless
one-off class definitions - but it seems to me that the subsequent
language evolution in favour of anonymous blocks brings fewer benefits
and can, as you say, diminish readability.

Of course, there are other similar areas in recent Python development.
Nested functions appear to eliminate the need for one-off classes:

def f(x):                # class f:
  def g(y):              #   def __init__(self, x):
    return op(x, y)      #     self.x = x
  return g               #   def __call__(self, y):
g = f(a)                 #     return op(self.x, y)
                         # g = f(a)

Generators could be replaced by classes whose objects would act as
state machines. Here, the readability permitted by the enhancement is
significantly greater than that permitted by the language prior to the
introduction of the enhancement, although there may be cases where
state machines are more instructive representations in understanding a

Generally, though, such enthusiasm for reducing the number of
keypresses involved in implementing something often seems to deny how
concise Python code already is in many situations.


More information about the Python-list mailing list