Do you need to exchange a variant array between Delphi and Python Quickly? Python4Delphi offers the TPythonModule component which has a method and procedure to exchange a variant array. This post guides you to do that. You can also use Python4Delphi with C++Builder.
Python4Delphi Demo17 App shows how to assign and get a variant array value using the TPythonModule component. You can find the Demo17 source on GitHub.
Prerequisites: Download and install the latest Python for your platform. Follow the Python4Delphi installation instructions mentioned here. Alternatively, you can check out this video Getting started with Python4Delphi.
Components used in Python4Delphi Demo16 App:
- TPythonEngine: A collection of relatively low-level routines for communicating with Python, creating Python types in Delphi, etc. It’s a singleton class.
- TPythonGUIInputOutput: Inherited from TPythonInputOutput (which works as a console for python outputs) Using this component Output property you can associate the Memo component to show the Output.
- TPythonModule inherited from TMethodsContainer class allows creating modules by providing a name. You can use routines SetVarFromVariant, GetVarAsVariant to set, and get a variant. You can create events using the Events property.
- TMemo: A multiline text editing control, providing text scrolling. The text in the memo control can be edited as a whole or line by line.
- PythonEngine1 provides the connection to Python or rather the Python API. This project uses Python3.9 which can be seen in TPythonEngine DllName property.
- PythonGUIInputOutput1 provides a conduit for routing input and output between the Graphical User Interface (GUI) and the currently executing Python script.
- PythonModule1 with the name test is created which is later imported and displays the variant array generated from a python sequence. You can use routines SetVarFromVariant, GetVarAsVariant to set, and get a variant. Internally this component converts a Delphi Variant array value to a python object and vice versa.
- On Clicking the Execute Script button, the variant comArray is created and set the variant array to PythonModule1. Later using python script written in memo1 executes and prints the output in memo2.
rocedure TForm1.Button1Click(Sender: TObject);
procedure DisplayArrayVar( V : Variant );
i, j : Integer;
item : Variant;
s : String;
Memo2.Lines.Add('Displaying a variant array generated from a Python sequence:');
for i := VarArrayLowBound( V, 1 ) to VarArrayHighBound( V, 1 ) do
item := V[i];
for j := VarArrayLowBound( item, 1 ) to VarArrayHighBound( item, 1 ) do
s := item[j];
Memo2.Lines.Add(Format('(%d, %d) = %s',[i, j, s]));
ComArray : Variant;
ComArray := VarArrayCreate([0, 3, 0, 2], varVariant);
ComArray[0, 0] := 1;
ComArray[0, 1] := 1.1;
ComArray[0, 2] := 'a';
ComArray[1, 0] := 2;
ComArray[1, 1] := 2.2;
ComArray[1, 2] := 'b';
ComArray[2, 0] := 3;
ComArray[2, 1] := 3.3;
ComArray[2, 2] := 'c';
ComArray[3, 0] := 4;
ComArray[3, 1] := 4.4;
ComArray[3, 2] := 'd';
PythonModule1.SetVarFromVariant( 'L', ComArray );
DisplayArrayVar( PythonModule1.GetVarAsVariant( 'L' ) );
Its that simple passing Delphi variant array to python and get the value back from python.