Executing a string

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Heshan Suri
    New Member
    • Apr 2008
    • 6

    Executing a string

    Hi,
    If I execute the following code I will get the the output as 2. What I need is to execute the string str. Is there a way that I can execute the string str as it is, so I will get the output as 5.

    Code:
    def add(a,b):
        return a+b
    
    var = 2
    str = "var = add(2,3)"
    [color=#4080FF]# execute the code snippet inside the str string variable[/color]
    print var
  • Smygis
    New Member
    • Jun 2007
    • 126

    #2
    [code=python]
    >>> expr = "var = (2+3)+pow(2,2)"
    >>> exec(expr)
    >>> var
    9
    [/code]

    Comment

    • jlm699
      Contributor
      • Jul 2007
      • 314

      #3
      As smygis silently pointed out, you should not use reserved words for variable declarations...
      In your example you used str = 'var = add(2,3)'... but if you wanted to type-cast a numeric variable you would not be able to do str(some_num) ...
      It would behoove you to try and avoid this in your python scripts from now on.

      Comment

      • raubana
        New Member
        • Feb 2008
        • 55

        #4
        It looks like you made the command part of the text:

        "a+b = var(a,b)"

        instead of

        "a+b =", var(a,b)

        One thing I'd do is convert the result into a string:

        [CODE=python]

        def add(a,b):
        * * return a+b

        var = 2
        str = "var =" + str( add(2,3) )
        [color=#4080FF]# execute the code snippet inside the str string variable[/color]

        print var

        [/CODE]

        That's all there is to it.

        Comment

        • jlm699
          Contributor
          • Jul 2007
          • 314

          #5
          Originally posted by raubana
          It looks like you made the command part of the text:

          "a+b = var(a,b)"
          That's what he wanted to do.

          Using exec() allows him to take a string and execute it as if the string was a command. This function is extremely helpful when working with a very dynamic program, as it allows you to do things similar to this:
          [code=python]>>> elem1 = 'ab'
          >>> elem2 = 'wtf'
          >>> elem3 = 'foo'
          >>> elem4 = 'bar'
          >>> elem5 = 'lol'
          >>> elem6 = 'abc'
          >>> for idx in range(1, 7):
          ... exec('print elem%d' % idx)
          ...
          ab
          wtf
          foo
          bar
          lol
          abc
          >>> [/code]
          Additionally the assignment could be taken care of in the same manner:
          [code=python]txts = ['ab', 'wtf', 'foo', 'bar', 'lawl', 'abc']
          for idx in range(1, 7):
          exec( "elem%d = '%s'" % ( idx, txts[idx - 1] ) )

          for idx in range(1, 7):
          exec( 'print elem%d' % idx )

          ab
          wtf
          foo
          bar
          lawl
          abc
          [/code]

          Comment

          Working...