Topics

Inconsistent return values


ddelapasse@...
 

Hi,

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!!!")



Markus Schaber
 

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.

 

 

 

Mit freundlichen Grüßen / Best regards

Markus Schaber


CODESYS Group
We
software Automation.


CODESYS Development GmbH
A member of the CODESYS Group

Dipl.-Inf.
Markus Schaber | Team Leader Automation Server
Tobias-Dannheimer-Str. 5 | 87439 Kempten | Germany
Tel. +49-831-54031-979
m.schaber@... | codesys.com

CODESYS Store: store.codesys.com
CODESYS Talk: forge.codesys.com
CODESYS Online Help: help.codesys.com


Geschäftsführer / CEOs: Dipl.-Inf. Dieter Hess, Dipl.-Inf. Manfred Werner
Handelsregister / Trade register: Kempten HRB 13379
| USt-IDNr. / Tax ID No.: DE 307355786


Diese E-Mail enthält möglicherweise vertrauliche und/oder rechtlich geschützte Informationen. Wenn Sie nicht der richtige Adressat sind
oder diese E-Mail irrtümlich erhalten haben, informieren Sie bitte sofort den Absender und vernichten Sie diese Mail.
Das unerlaubte Kopieren sowie die unbefugte Weitergabe dieser Mail ist nicht gestattet.

This e-mail may contain confidential and/or privileged information. If you are not the intended recipient or have received
this e-mail in error, please notify the sender immediately and destroy this e-mail. Any unauthorized copying, disclosure
or distribution of the material in this e-mail is strictly forbidden.

Von: users@ironpython.groups.io <users@ironpython.groups.io> Im Auftrag von ddelapasse@...
Gesendet: Freitag, 23. Oktober 2020 21:59
An: users@ironpython.groups.io
Betreff: [ironpython] Inconsistent return values

 

Hi,

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!!!")

 


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