using lambda to print everything in a list

Marcin 'Qrczak' Kowalczyk qrczak at knm.org.pl
Sat Apr 28 11:21:55 EDT 2001


Sat, 28 Apr 2001 13:33:22 +0200, Christian Tanzer <tanzer at swing.co.at> pisze:

>     map (lambda s : s.capitalize (), l)
> 
> I wouldn't call a loop clearer in this case, despite the lambda.
> 
> For strings, one could of course pass `string.capitalize' to `map'.
> Unfortunately, this loop hole exists only for strings, and the
> powers that be plan on making the string module obsolete, anyway.

You can abstract away the pattern of calling a method on the argument
(but it's probably too tricky to be generally liked):


from __future__ import nested_scopes

class Arg:
    def __getattr__(self, name):
        return lambda *args, **kwargs: lambda obj: getattr(obj, name)(*args, **kwargs)
arg = Arg()

map(arg.capitalize(), l)


Unfortunately it works only for called methods, i.e.
    map(arg.field, l)
won't work.

-- 
 __("<  Marcin Kowalczyk * qrczak at knm.org.pl http://qrczak.ids.net.pl/
 \__/
  ^^                      SYGNATURA ZASTĘPCZA
QRCZAK



More information about the Python-list mailing list