Array of functions, Pythonically
Bruno Desthuilliers
bruno.42.desthuilliers at wtf.websiteburo.oops.com
Mon Feb 25 11:16:20 EST 2008
MartinRinehart at gmail.com a écrit :
> My parser has found an expression of the form CONSTANT_INTEGER
> OPERATOR CONSTANT_INTEGER. I want to fold this into a single
> CONSTANT_INTEGER.
>
> The OPERATOR token has an intValue attribute, '+' == 0, '-'== 1, etc.
> In C I'd put functions Add, Subtract, ... into an array and call
> ArithmeticFunctions[ intValue ] to perform the operation. Would I
> index into a list of functions in Python, or would IF/ELIF/ELIF ... be
> the way to go?
Python functions are first class objects, so you can indeed have a list
(or dict etc) of functions:
funcs = [lambda x : x+1, lambda x : x * 42]
funcs[0](1)
funcs[1](1)
More information about the Python-list
mailing list