deref in a string into an operand

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John Doe

    #1

    deref in a string into an operand

    I've got the action '+' stored as the variable 'plus',
    that is plus='+'

    Now I want to use the variable plus in an equation:
    5 plus 7, just line 5 + 7

    How do I do that?

    Dan

  • John Doe

    #2
    Re: deref in a string into an operand

    Foolish me; I examined the __builtin__ and found __getattribute_ _,
    along with '__mul__' '__add__', '__sub__' and '__div__'.

    So using a builtin object like 'x' ...

    x = 5
    attr = '__mul__'

    print x.__getattribut e__( attr )( 5 )
    25

    My program will store the operand as '__mul__' instead of '*', along with
    a health bit of comment.

    Dan. Closed.

    On Fri, 05 Nov 2004 08:01:59 +0000, John Doe wrote:
    [color=blue]
    > I've got the action '+' stored as the variable 'plus',
    > that is plus='+'
    >
    > Now I want to use the variable plus in an equation:
    > 5 plus 7, just line 5 + 7
    >
    > How do I do that?
    >
    > Dan[/color]

    Comment

    • Peter Otten

      #3
      Re: deref in a string into an operand

      John Doe wrote:
      [color=blue]
      > Foolish me; I examined the __builtin__ and found __getattribute_ _,
      > along with '__mul__' '__add__', '__sub__' and '__div__'.
      >
      > So using a builtin object like 'x' ...
      >
      > x = 5
      > attr = '__mul__'
      >
      > print x.__getattribut e__( attr )( 5 )
      > 25
      >
      > My program will store the operand as '__mul__' instead of '*', along with
      > a health bit of comment.[/color]

      However, this may sometimes fail:
      [color=blue][color=green][color=darkred]
      >>> 1 + 1.0[/color][/color][/color]
      2.0[color=blue][color=green][color=darkred]
      >>> 1 .__add__(1.0)[/color][/color][/color]
      NotImplemented

      Consider using the operator module instead:
      [color=blue][color=green][color=darkred]
      >>> import operator
      >>> operator.add(1, 1.0)[/color][/color][/color]
      2.0[color=blue][color=green][color=darkred]
      >>> getattr(operato r, "add")(1, 1.0)[/color][/color][/color]
      2.0

      Peter

      Comment

      • Steven Bethard

        #4
        Re: deref in a string into an operand

        John Doe <atterdan <at> yahoo.com> writes:[color=blue]
        >
        > So using a builtin object like 'x' ...
        >
        > x = 5
        > attr = '__mul__'
        >
        > print x.__getattribut e__( attr )( 5 )
        > 25
        >
        > My program will store the operand as '__mul__' instead of '*', along with
        > a health bit of comment.[/color]

        You also might consider just storing the operator.<op> methods in an appropriate
        dictionary:
        [color=blue][color=green][color=darkred]
        >>> import operator
        >>> operator_map = {'+':operator.a dd, '-':operator.sub}
        >>> operator_map['+'](1, 2.0)[/color][/color][/color]
        3.0

        This allows you to call the add function whatever you want.

        Steve

        Comment

        • Peter Abel

          #5
          Re: deref in a string into an operand

          John Doe <atterdan@yahoo .com> wrote in message news:<pan.2004. 11.05.08.00.19. 364836@yahoo.co m>...[color=blue]
          > I've got the action '+' stored as the variable 'plus',
          > that is plus='+'
          >
          > Now I want to use the variable plus in an equation:
          > 5 plus 7, just line 5 + 7
          >
          > How do I do that?
          >
          > Dan[/color]

          If you want a string:
          equation = '5 '+plus+' 7'

          or

          equation = '5 %s 7' % plus

          Regards
          Peter

          Comment

          Working...