Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return a list to Python from Delphi using Python4Delphi

Using Python4Delphi, it is fairly straight forward to expose Delphi methods to Python so that Python can call into a Delphi application. However I have been unable to return a Python list that is created by a Delphi method to Python. For example:

function TDelphiAPI.callMethod : PPyObject;
begin
  // Create a new empty list of three elements
  result := GetPythonEngine.PyList_New(3);
end;

import mylib
p = mylib.DelphiAPI()
print p.callmethod()

This returns 'NoneType' when called from Python. If PPyObject is changed to a type such as integer, AnsiString or double, Python picks up the correct type and displays it correctly. I use

  GetPythonEngine.AddMethod ('callMethod', @TDelphiAPI.callMethod, 'callMmethod)');

to expose the method from Delphi. However there is no way to specify the types for either the arguments or return type, as far as I can tell.

I am wondering if anyone has returned from delphi python types such as lists or even numpy arrays?

like image 236
rhody Avatar asked Nov 08 '22 11:11

rhody


1 Answers

Normal Python arrays (as for standard CPython) are normally called "Lists". A numpy.array type (or a mutable list) in Python is a special type that is more memory and layout efficient than a normal Python list of normal Py floating point objects. If you want to use Delphi and access Numpy.array or list, I suppose that the straightest way to do it would be to implement a way to export some simple C functions that access the Numpy.array type.

Numpy.array wraps a standard block of memory that is accessed as a native C array type. This in turn, does NOT map cleanly to Delphi array types as created by a Delphi method to Python.

like image 181
Max Kleiner Avatar answered Nov 15 '22 07:11

Max Kleiner