explain this function to me, lambda confusion

Arnaud Delobelle arnodel at googlemail.com
Mon May 19 12:04:58 EDT 2008


Paul McGuire <ptmcg at austin.rr.com> writes:

[...]

> lambda is handy in defining parse actions in pyparsing.  Parse actions
> are callbacks to be run when an expression within a larger grammar is
> matched.  A common use for parse actions is to do some sort of text or
> type conversion.  The simplest parse actions are called using the list
> of matched tokens.  Here is a subexpression that will convert numeric
> strings found in a larger grammar to ints:
>
> integer = Word("0123456789").setParseAction(lambda tokens:
> int(tokens[0]) )

Could you use it as a decoratore instead?

integer = Word("0123456789")

@integer.setParseAction
def parse_integer(tokens):
    return int(tokens[0])

I could make your grammar clearer, because you don't mix it with
processing code... and no need for lambdas!

-- 
Arnaud



More information about the Python-list mailing list