Python C API: why doesn't PyRun_String evaluate simple conditional expressions?

That isn't a conditional expression: it's a statement Py_eval_input means to treat the string as a single expression. You probably want Py_single_input to treat the string as a statement.

That isn't a conditional expression: it's a statement. Py_eval_input means to treat the string as a single expression. You probably want Py_single_input to treat the string as a statement.

This is the same as the distinction in Python code between eval (which is what you asked for) and exec. I am assuming of course that the statement you actually want to execute will be slightly more complex otherwise there isn't much point using either eval or exec. For exec you'll want to make sure it has side effects so you can tell the result, e.g.By binding some value to a name.

I'm new to python. I tried "1 if True else 0" and it works. Thanks.

– David May 27 at 9:22 I thought of suggesting that but wrongly assumed that you wouldn't be playing with the C-api until you had some experience with Python itself. BTW, if you are doing more than just running a simple expression or statement consider using Cython instead of using the C-api directly. – Duncan May 27 at 9:38.

It does, but you're not doing anything that will produce output or return a value. Consider the following code: #! /usr/bin/python def foo(): if True: 1 else: 0 a = foo() print(a) a will not get the value 0 or 1 - it will be 'None'.

This would be the problem if there was no output, but not the source of this particular exception. – Thomas Wouters May 27 at 9:21.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions