Lambda going out of fashion

rzed jello at comics.com
Thu Dec 23 08:36:08 EST 2004


Stephen Thorne <stephen.thorne at gmail.com> wrote in
news:mailman.8299.1103775211.5135.python-list at python.org: 

> Hi guys,
> 
> I'm a little worried about the expected disappearance of lambda
> in python3000. I've had my brain badly broken by functional
> programming in the past, and I would hate to see things suddenly
> become harder than they need to be.
> 
> An example of what I mean is a quick script I wrote for doing
> certain actions based on a regexp, which I will simlify in this
> instance to make the pertanant points more relevent.
> 
> {
>  'one': lambda x:x.blat(),
>  'two': lambda x:x.blah(),
> }.get(someValue, lambda x:0)(someOtherValue)
> 
> The alternatives to this, reletively simple pattern, which is a
> rough parallel to the 'switch' statement in C, involve creating
> named functions, and remove the code from the context it is to
> be called from (my major gripe).
>
> So, the questions I am asking are: 
> Is this okay with everyone? 
> Does anyone else feel that lambda is useful in this kind of
> context? Are there alternatives I have not considered?
> 

Not addressing lambdas per se, but something similar to your pseudo-
switch statement can be done without using them at all. One way might 
be to use a function dictionary wrapped in an accessor function, such 
as this:

def dofns(key):
    fd2 = {
    0:'print "Key 0"',
    1:'print "one"',
    4:"\n".join(['for ix in range(15):',
    	    	'  print "%s%d" % (" "*ix,ix)']),
    }
    try:
        exec(fd2[key])
    except KeyError:
        print 'Key',key,'not found'

The keys can as easily be strings, of course. One virtue of this 
method is that it allows multi-line statements and looping (as in fd2
[4]).

Now the actual 'switch' statement becomes as simple as:
    	dofns(key)

If there are parameters involved, this solution becomes a little 
tackier. One way might be something like:

def dofns(key,**kws):
    fd2 = {
    0:'print "Key 0"',
    1:'print "one"',
    2:"\n".join(['n=kws["name"]','x=kws["x"]',
    	    	'y=kws["y"]','print "%s:"%n,x,y']),
    3:"\n".join(['val=kws["x"]*7 + kws["y"]',
    	    	'print kws["x"],kws["y"],val']),
    4:"\n".join(['for ix in range(kws["x"]):',
    	    	'  print "%s%d" % (" "*ix,ix)']),
    5:'exec(fd2[3])',
    6:"\n".join(['print kws["z"],',
    	    	'dofns(3,x=13,y=22)']),
    }
    try:
        exec(fd2[key])
    except KeyError:
        print 'Key',key,'not found'


# switch (key kwdparms)
for key in [0, 1,2,3,4,5,6,7]:
    dofns(key,name='Gladys',y=50,x=8,z=34)

You could remove the function dictionary from the wrapper and use it 
similarly to your example, but I find that less readable. 
    	exec({
    	    	multi-line dictionary definition
    	    	}[key] parms) # what were we doing again? Oh! exec!

Anyway, for those who use lambdas, this approach is likely to be 
unappealing. For those who find lambdas puzzling, it may be an 
alternative, at least for a way to handle the switch equivalent.

-- 
rzed




More information about the Python-list mailing list