Python Shell - Grab last expression output and put back into variable. An Underscore Tip
Excuse if this is obvious to Python programmers but for those crossing over from ObjectScript this may be a useful tip.
The scenario is developing some Embedded python commands.
Testing out functionality is being confirmed via the shell:
$SYSTEM.Python.Shell() Python 3.9.5 (default, Mar 14 2023, 06:58:44) [MSC v.1927 64 bit (AMD64)] on win32 Type quit() or Ctrl-D to exit this shell. >>>
When Python evaluates an expression in the shell, it prints the result of the expression to the terminal.
>>> 1 + 2 3
It is quite easy to accidentally evaluate and print out values
>>> iris.cls("%Dictionary.MethodDefinition")._OpenId("%Library.String||LogicalToJSON",0)
<iris.%Dictionary.MethodDefinition object at 0x000001C55C8572D0>
# .. but I really wanted that object for further workie: What I really wanted to do was set a new variable to return value from that function call:
>>> myvar = iris.cls("%Dictionary.MethodDefinition")._OpenId("%Library.String||LogicalToJSON",0)This can cause delay if there was a lot of setup involved to get to this point.
To the rescue is Hero variable "_" underscore. This holds the value of last evaluated expression for you.
You can save you R&D moment by executing: "[variable name] = _ "
>>> iris.cls("%Dictionary.MethodDefinition")._OpenId("%Library.String||LogicalToJSON",0)
<iris.%Dictionary.MethodDefinition object at 0x000001C55C8572D0>
# oops
>>> myvar = _
# Keep calm and carry on ...
>>> myvar._ClassName()
'MethodDefinitionHope this is useful for someone else.