· Apr 10

get multiple return value in embeded python


I am using embeded python to utilize some pythonic library but i got a problem on my hand.

One of the python function i am using return multiple values

in python you would do something like that :

val1, val2, val3, = function(params)

In COS I got something like that :

lib = ##class(%SYS.Python).Import("lib")
val1 = lib.function(params)

And I don't know how to get the second and third values.
Is there a way to get them?

Product version: IRIS 2023.3
Discussion (5)2
Log in or sign up to continue

I have a limited embedded python experience but if you can manage to get a (COS)%List from python then the solution is:

set $lb(val1, val2, val2, ...) = ConvertPythonTupelToCOSList(pythonfunction(param))
set $lb(val1, val2, val2, ...) = ConvertPythonListToCOSList(pythonfunction(param))

/// For example
ClassMethod Test()
  return $lb(11,22,33)

set $lb(a,b,c)=##class(Some.Class).Test()
write a,?5,b,?10,c ---> 11   22   33

I know this isn't helpful but it knocks one solution OFF the list.  I thought you could use Outputs in your method signature but this isn't supported with Python methods:

While passing arguments by reference is a feature of ObjectScript methods, there is no equivalent way to pass arguments by reference to a method written in Python. The ByRef and Output keywords in the signature of an ObjectScript method are conventions used to indicate to the user that the method expects that an argument is to be passed by reference. In fact, ByRef and Output have no actual function and are ignored by the compiler. Adding ByRef or Output to the signature of a method written in Python results in a compiler error.

From your example, I think you'd want to fire up the Python shell from the COS command line and import the iris package and call your function with the normal multiple return value syntax.  If you import the iris package you can then save the output to a global or pass it to another COS function that you need it for.

For this given python function :

file name

def return_tuple():
    return 1, 2, 3

To retrieve the values in ObjectScript, you can use the following code, basically use dunder methods to access the values.

set return = ##class(%SYS.Python).Import("demo")."return_tuple"()
write return."__len__"() // 3
write return."__getitem__"(0) // 1
write return."__getitem__"(1) // 2
write return."__getitem__"(2) // 3