DelphiLearn PythonPythonPython GUIRAD Studio

Learn About Powerful Python Decorators In A Delphi Windows GUI App

blogbanner3 9

Python has an interesting feature called Decorators to add functionality to an existing code. A Decorator is a design pattern in Python that allows a user to add new functionality to an existing object without modifying its structure. Decorators are usually called before the definition of a function you want to decorate. This is also called metaprogramming because a part of the program tries to modify another part of the program at compile time.

This post will demonstrate how to run another Python feature in Python4Delphi with RAD Studio: Decorators in Python GUI apps and gets the output.

Prerequisites: Before we begin to work, download and install the latest Python for your platform. Follow the Python4Delphi installation instructions mentioned here. Alternatively, you can check out the easy instructions found in this video Getting started with Python4Delphi.

First, open and run our Python GUI using project Demo1 from Python4Delphi with RAD Studio. Then insert the script into the lower Memo, click the Execute button, and get the result in the upper Memo. You can find the Demo1 source on GitHub. The behind the scene details of how Delphi manages to run your Python code in this amazing Python GUI can be found at this link.

0 rundemo1
Open Demo01dproj

1. Creating Decorators in Python

Let’s go ahead and create a simple decorator that will convert a sentence to uppercase. We do this by defining a wrapper inside an enclosed function:

Our decorator function takes a function as an argument, and we shall, therefore, define a function and pass it to our decorator. We learned earlier that we could assign a function to a variable. We’ll use that trick to call our decorator function.

Note that in Python GUI by Python4Delphi, to print the result, you need to state the print() function, it’s not enough just by return statement.

Output in Python GUI:

1 1 1123516
Simple Example of Python Decorator that will Convert a Sentence to Uppercase

However, Python provides a much easier way for us to apply decorators. We simply use the @ symbol before the function we’d like to decorate. Let’s see that in the practice below:

1 2 5977767
Very Much Simpler Example of Python Decorator that will Convert a Sentence to Uppercase in Python4Delphi

 

2. Applying Multiple Decorators to a Single Function in Python

We can use multiple decorators to a single function. However, the decorators will be applied in the order that we’ve called them. Below we’ll define another decorator that splits the sentence into a list. We’ll then apply the uppercase_decorator and  split_string decorators to a single function:

The output in Python GUI:

2 7762728
Applying Multiple Decorators to a Single Function in Python

From the above output, we notice that the application of decorators is from the bottom up. Since lists don’t have an upper attribute, the sentence has first been converted to uppercase and then split into a list.

 

3. Accepting Arguments in Decorator Functions

Sometimes we might need to define a decorator that accepts arguments. We achieve this by passing the arguments to the wrapper function. The arguments will then be passed to the function that is being decorated at call time.

Output:

3 8859775
Accepting Arguments in Decorator Functions in Python

 

4. Summary

Decorators dynamically alter the functionality of a function, method, or class without having to directly use subclasses or change the source code of the function being decorated. Using decorators in Python also ensures that your code is DRY(Don’t Repeat Yourself). Decorators have several use cases such as:

  • Authorization in Python frameworks such as Flask and Django
  • Logging
  • Measuring execution time
  • Synchronization

Check out Python4Delphi which easily allows you to build Python GUIs for Windows using Delphi.

Related posts
CodeIDELearn PythonPythonPython GUITkinter

How To Make More Than 20 ChatGPT Prompts Work With Python GUI Builders And Matplotlib Library?

CodeIDELearn PythonPythonPython GUITkinter

How To Make More Than 20 ChatGPT Prompts Work With Python GUI Builders And Pillow Library?

CodeDelphiDelphiFMXLearn PythonProjectsPythonPython GUI

How To Create A Weather App With The Python Delphi Ecosystem and Weatherstack API

CodeDelphiDelphiFMXLearn PythonProjectsPythonPython GUI

How To Create A Music Player With The Python Delphi Ecosystem

Leave a Reply

Your email address will not be published. Required fields are marked *