Re: Inconsistent return values


Pavel Koneski
 

I'm relatively new to ironpython and trying to understand my options for returning values back to my C# app. Can someone explain why option 1 returns fine, but option 2 returns null. Trying to allow my web app users to code up some "formulas" (basic logic) and this seems like a great option, but I need to understand "the rules". The responses will be sometimes text and sometimes numeric so wrapping in json.dumps seems like a good plan.

Thanks!!!
Deanna

-------- option 1 (returns "last line") ----------
import json
json.dumps("last line")

--------- option 2 (returns null) -----------
import json
if True==True:
json.dumps("true!!!")
else:
json.dumps("false!!!”)
On 27 Oct, 2020, at 01:27, Markus Schaber <m.schaber@...> wrote:

Hi, Deanna,

I think it‘s the difference between a statement and an expression.

Statements don’t have return values in general, only if they’re expressions. “if”-statements are not expressions.

My guess is: If the last statement is an expression, it uses it’s result as return value, and None otherwise.
Frankly it looks like a bug to me. Statements have a value, although it is often None, the value of if/else should be the value of the last statement in the executed branch.

As a workaround, you may try:

```
import json
if True==True:
res = json.dumps("true!!!")
else:
res = json.dumps("false!!!”)
res
```

Deanna, you may get a more decisive response on the Gitter chat[1], which is more active than this mailing list.

Regards,
Pavel Koneski

[1]: https://gitter.im/IronLanguages/ironpython

Join users@ironpython.groups.io to automatically receive all group messages.