Easily Create A Python Container Type In Delphi And Execute It In This Windows Sample App

Earlier we learned how to create a Python type using Delphi classes. How about creating a Python type with some containers or collections capabilities in Delphi and accessing its elements? Python4Delphi PyObject contains Type Services routines e.g Basic, Number, Sequence, Mapping which can be overridden with our custom Python Types Delphi classes. This post helps to do that.

Python4Delphi Demo27 Sample App shows how to create a Module, Python type, Import the module, and Python Type in a python script, create a sequence type object and access some of the service routines like length, indexing, etc. You can find the Demo27 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 Demo27 Sample 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: It’s inherited from TMethodsContainer class allows creating modules by providing a name. You can use routines AddMethod, AddMethodWithKW, AddDelphiMethod, AddDelphiMethodWithKeywords to add a method which should be type compatible with this routine parameter. You can create events using the Events property.
  • TPythonType: This component helps to create a new python type in Delphi which is inherited from the hierarchy of classes (set of APIs to create, manage methods, and members).
  • 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.

You can find the Python4Delphi Demo27 sample project from the extracted repository ..Python4DelphiDemosDemo27.dproj. Open this project in RAD Studio 10.4.1 and run the application.

Implementation Details:

  • 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 a Module name test is created.
  • In this sample, Created a Delphi class(TMySeq) implementing a new Python type. It is derived from TPyObject. It overrides some Mapping services virtual methods such as MpLength, MpSubscript.
  • During PythonType1 initialization assign the property PyObjectClass with the class TMySeq.
  • On Clicking Execute Button the below code is executed.

Which will execute the python script mentioned below.

Note. CreateMySeq in the above script is created automatically by Python4Delphi when the property GenerateCreateFunction of TPythonType1(MySeq) is true.

demo27-6357081
Python4Delphi Demo27