[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Array of functions, Pythonically

Martin Rinehart

2/25/2008 2:23:00 PM

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?
4 Answers

Duncan Booth

2/25/2008 2:56:00 PM

0

MartinRinehart@gmail.com wrote:

> 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?
>
Well, you could add another attribute to the OPERATOR token:

token.operation = operator.add

or you could have an a list of operations keyed in token.intValue:

[ operator.add, operator.sub, ...]

or you could use a dictionary (that intValue attribute is just so C):

{ '+': operator.add, '-': operator.sub, ... }

I don't know the structure of your program, but I'd go for subclassing
your operator. So you would have classes OperatorAdd, OperatorSub, etc.
and then you can make the operation a method of the class. Seomthing
like:

class Token(object): ...
def foldConstants(self): pass

class Operator(Token): ...
def foldConstants(self):
self.left.foldConstants()
self.right.foldConstants()
if self.left.isConstant and self.right.isConstant:
self.value, self.isConstant = self.doFold()


class OperatorAdd(Operator): ...
def doFold(self):
return self.left.value + self.right.value, True

Bruno Desthuilliers

2/25/2008 4:16:00 PM

0

MartinRinehart@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)

Aaron Brady

2/25/2008 5:54:00 PM

0

>  { '+': operator.add, '-': operator.sub, ... }

Then EXPR OPER EXPR -> ops[ OPER ]( EXPR, EXPR ), right?

Paul McGuire

2/25/2008 6:03:00 PM

0

On Feb 25, 11:53 am, castiro...@gmail.com wrote:
> >  { '+': operator.add, '-': operator.sub, ... }
>
> Then EXPR OPER EXPR -> ops[ OPER ]( EXPR, EXPR ), right?

I think this is the most Pythonic idiom. You can even define your own
custom binary operators, such as '$' to convert dollars and cents to a
float:

>>> ops = { '+': operator.add, '-': operator.sub, '$' : lambda a,b: a + b/100.0 }
>>> ops['$'](1,2) # convert dollars and cents to float
1.02

-- Paul