Array of functions, Pythonically

Discussion in 'Python' started by MartinRinehart@gmail.com, Feb 25, 2008.

  1. Guest

    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?
     
    , Feb 25, 2008
    #1
    1. Advertising

  2. Duncan Booth Guest

    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
     
    Duncan Booth, Feb 25, 2008
    #2
    1. Advertising

  3. 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)
     
    Bruno Desthuilliers, Feb 25, 2008
    #3
  4. Guest

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

    Then EXPR OPER EXPR -> ops[ OPER ]( EXPR, EXPR ), right?
     
    , Feb 25, 2008
    #4
  5. Paul McGuire Guest

    On Feb 25, 11:53 am, 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
     
    Paul McGuire, Feb 25, 2008
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Xiangliang Meng
    Replies:
    1
    Views:
    1,623
    Victor Bazarov
    Jun 21, 2004
  2. Brendan
    Replies:
    10
    Views:
    587
    Stefan Rank
    Nov 8, 2005
  3. Fried Egg
    Replies:
    1
    Views:
    282
    bruno at modulix
    Jan 30, 2006
  4. Mara Guida

    const and array of array (of array ...)

    Mara Guida, Sep 2, 2009, in forum: C Programming
    Replies:
    3
    Views:
    498
    David RF
    Sep 3, 2009
  5. Tom
    Replies:
    3
    Views:
    215
    salsablr
    Dec 20, 2004
Loading...

Share This Page