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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With