Python is an incredibly popular general purpose object oriented language that can be used in many ways. Not only is it used by experts in the field of Data Science and Data Analytics, it is also used by many Universities as a means for teaching beginners. The power of Python can grow exponentially when it is paired with the right tools and that brings us to Python4Delphi which is a remarkable set of components that work together by wrapping up the Python DLL into Delphi and C++Builder thus enabling its users to view GUI for their Python scripts with ease. In the following tutorial, we will demo the methods used to define and user partial functions with the aide of P4D’s excellent GUI support.
Prerequisites: Before we begin working, it is essential you Download the latest version of Python for your platform. You can find the installation instructions for Python4Delphi at this link. Alternatively, you can follow the easy instructions found in this video Getting started with Python4Delphi.
Now in order to demonstrate P4D GUI, we must proceed by creating a VCL application. This can be achieved by running the Demo1 project available at the following gitHub repository link. When we run the Demo1 project, it will result in a VCL application titled “Demo of Python”. This application will have two main visual components called Memos. The top Memo will display code whereas the lower Memo will serve as an input area for the Python script by its users.
If you would like to know more about what goes on inside Delphi, in order to create this VCL application you can check out this wonderful article explaining the workings of the Demo1 project here.
The Python Code:
The ability to call Partial functions has been packaged within the Functools library of Python. It is incredibly useful when dealing with code that requires flexibility. Partial functions allow the developers to call normal functions with some of the arguments already filled. So for example if the developer has to to use a function multiple times but only needs to change one particular argument, they can utilize the power of Partial functions. Let’s take a look at this in code.
1 2 3 4 5 6 7 8 9 10 11 12 |
from functools import partial #Defining a function def func1(arg1, arg2, arg3): return arg1 + arg2 + arg3 #Defining a partial function "part" # "part" will call func1 with arg1 and arg2 fixed as 1 and 2. part = partial(func1, 1, 2) #Calling "part" print(part(3)) |
but what if we want to fix the third argument, keeping the first two arguments programmable? The solution to this would be simple. We will make a simple change in the partial function, keeping everything else the same. Observe how this fixes the third argument whilst keeping the other two, flexible.
1 2 3 4 5 6 7 8 9 10 11 12 |
from functools import partial #Defining a function def func1(arg1, arg2, arg3): return arg1 + arg2 + arg3 #Defining a partial function "part" # "part" will call func1 with arg1 and arg2 fixed as 1 and 2. part = partial(func1, arg3 = 1) #Calling "part" print(part(2,3)) |
In the screenshot below, “Part 1” is the result from the first code snippet. Whereas “Part 2” is the result from the second code snippet.
Check out some more cool tutorials on Python4Delphi over here.