2

I want to write a Python string that when executed does this:

if condition:
    consequence
else:
    alternative

So, I tried something like:

string = 'if condition: consequence; else: alternative;'

to be executed with:

>>> exec(string)
Traceback (most recent call last):
  File "<pyshell#5>", line 1, in <module>
    exec(string)
  File "<string>", line 1
    if condition: consequence; else: alternative;
                                  ^
SyntaxError: invalid syntax

But as you see, I get a syntax error. How should the string be formatted?

Thanks for the help!

PS: this question is not about how to evaluate or execute Python strings (See instead How do I execute a string containing Python code in Python?), but about the formatting required to execute an if..then..else clause.

Some context:

I am following the book "understanding computation" by Tom Stuart. Part of it is about understanding how programming languages work. So he gives example code for the implementation of a toy language 'SIMPLE'. He shows code in Ruby how to translate SIMPLE into Ruby code. However, I am trying to write this in Python, as that interests me more. The Ruby example is:

def to_ruby 
    "-> e { if (#{condition.to_ruby}).call(e)" + 
          " then (#{consequence.to_ruby}).call(e)" + 
          " else (#{alternative.to_ruby}).call(e)" + 
          " end }"
end
14
  • 2
    Are you sure that you need to use eval or exec? Using eval or exec is often a bad idea. What's the overall goal of this code? Commented Dec 11, 2015 at 1:50
  • I am following the book "understanding computation" by Tom Stuart. Part of it is about understanding how programming languages work. So he gives example code for the implementation of an toy language 'SIMPLE'. He shows code in Ruby how to translate SIMPLE into Ruby code. However, I am trying to write this in Python, as that interests me more. The Ruby example is: def to_ruby "-> e { if (#{condition.to_ruby}).call(e)" + " then (#{consequence.to_ruby}).call(e)" + " else (#{alternative.to_ruby}).call(e)" + " end }" end Commented Dec 11, 2015 at 1:54
  • I can't get the line breaks right in the previous comment. They're basically before each opening ". Commented Dec 11, 2015 at 2:00
  • you can use exec to execute a string but in this case you'd be better off using conditional string formatting Commented Dec 11, 2015 at 2:05
  • 3
    Possible duplicate of How do I execute a string containing Python code in Python? But remember use it safely! Commented Dec 11, 2015 at 2:13

2 Answers 2

2

You can use exec to execute Python statements, including newlines:

>>> exec("if True:\n  print 'abc'\nelse:\n  print 'def'")
abc

If using Python 3, you would need parentheses for the prints:

>>> exec("if True:\n  print('abc')\nelse:\n  print('def')")
abc
Sign up to request clarification or add additional context in comments.

Comments

2

Based on the input by Kevin Guan and Tom Karzes, I have found following alternative solution to the problem:

>>> exec("""
if True:
    print('abc')
else:
    print('def')
""")
abc

This format avoids the somewhat annoying \n symbols.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.