Site icon Embarcadero RAD Studio, Delphi, & C++Builder Blogs

Learn How To Build An Iterator Python Type For Delphi In This Windows GUI App

We are aware of how to use TStringList in Delphi. we learned how to create a Python type using Delphi classes. Thinking How to create an iterator in Delphi Which holds python objects? This post guide you to create a Python Type that contains a list of strings(python string objects) similar to TStringList. And a String List Iterator Python Type In Delphi to iterate the StringList Python Type easily in Delphi with Python4Delphi Sample App.

Python4Delphi Demo28 Sample App shows how to create a StringList Python type, StringList Iterator Python Type in a Python Module, Import the module, and Iterator Python Types in a python script, create a Stringlist object and access some of the service routines like Iter, IterNext, etc. You can find the Demo28 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 Demo28 Sample App:

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

Implementation Details:

[crayon-673fb269e5443281690934/]

In this sample, TPyStringList overrides the Iter, Basic service of TPyObject, and Sequence Services such as SqLength, SqItem, SqAsItem. Using RegisterMethods exposed an Add method(Using this a string can be added to the string list). Used PyArg_ParseTuple to retrieve the arguments passed in python scripts. This class also has the property to return TStringList.

[crayon-673fb269e5451659230774/]

TPyStringListIterator overrides Iter, IterNext Basic services of TPyObject. Using RegisterMethods exposed a Next method(Using this the next item in the TPyStringList can be retrieved).

[crayon-673fb269e5453303502177/]

On Clicking Execute Button the below python scripts is executed. These 2 Python Types were instantiated in the python scripts and accessed as shown below.

[crayon-673fb269e5455867753044/]

Note. CreateTStringList in the above script is defined automatically by Python4Delphi when the property GenerateCreateFunction of ptStringList is true.

<strong>Python4Delphi Demo28<strong>
Exit mobile version