Have an amazing solution built in RAD Studio? Let us know. Looking for discounts? Visit our Special Offers page!
C++CodeDelphi

Learn How To Use Python Functions With Keyword Arguments In A Delphi Windows App

In Delphi, we pass values to parameters while calling a function in an order the function parameters were defined. This is the same in Python as well called positional arguments. However, Python has additional features passing arguments to the function with Keyword(Named) Arguments. To know more about these Keyword Arguments check here. How to use such Python functions in Delphi? This post will help to understand with Python4Delphi sample app.

Python4Delphi Demo30 Sample App shows how to define a Function with Keyword parameters in Python Main Module, Instantiate in Delphi with Keyword arguments and assert it by accessing the values. You can find the Demo30 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 Demo30 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.
  • 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 Demo30 sample project from the extracted repository ..Python4DelphiDemosDemo30.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 the TPythonEngine DllName property.
  • PythonGUIInputOutput1 provides a conduit for routing input and output between the Graphical User Interface (GUI) and the currently
    executing Python script.
  • In this sample, A Python function is defined with mixed positional parameters and keyword parameters in Memo1 lines which is executed by clicking the ‘Execute Script‘ Button.

Followed by the above script execution, the Person instance is instantiated within the MainModule by passing different keyword arguments in a different order. Each time the value is asserted and the code is shown below. You can ignore to pass any arguments in the available arguments or pass in any order.

 

demo30-1683255
Python4Delphi Demo30

Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial   Upgrade Today

   Free Delphi Community Edition   Free C++Builder Community Edition

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

IN THE ARTICLES