Flexible Way To Set And Get Variant Array Between Delphi and Python For Building Windows Apps

rocedure TForm1.Button1Click(Sender: TObject);

 

  procedure DisplayArrayVar( V : Variant );

  var

    i, j : Integer;

    item : Variant;

    s : String;

  begin

    Memo2.Lines.Add(‘Displaying a variant array generated from a Python sequence:’);

    for i := VarArrayLowBound( V, 1 ) to VarArrayHighBound( V, 1 ) do

      begin

        item := V[i];

        for j := VarArrayLowBound( item, 1 ) to VarArrayHighBound( item, 1 ) do

          begin

            s := item[j];

            Memo2.Lines.Add(Format(‘(%d, %d) = %s’,[i, j, s]));

          end;

      end;

  end;

 

var

  ComArray : Variant;

begin

  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 );

  PythonEngine1.ExecStrings(Memo1.Lines);

  DisplayArrayVar( PythonModule1.GetVarAsVariant( ‘L’ ) );

end;