Site icon Python GUI

Easily Learn Python Closures With A Delphi Windows GUI App

blogbanner3 8

A Closure in Python is a function object that remembers values in enclosing scopes even if they are not present in memory.

This post will demonstrate how to run another Python feature in Python4Delphi with RAD Studio: Closure 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.

Open Demo01dproj

 

1. Non-Local Variable in a Nested Function

Before reviewing deeper into what a Python Closure is, we need to understand what is a nested function and nonlocal variable first.

A function defined inside another function is called a nested function. Nested functions can access variables of the enclosing scope.

In Python, these non-local variables are read-only by default and we must declare them explicitly as non-local (using nonlocal keyword) in order to modify them.

Following is an example of a nested function accessing a non-local variable in Python GUI:

[crayon-662891caa735c447584115/]

Output in Python GUI:

Simple Example of Nested Function in Python GUI

 

2. Python Closures

Closures in Python are created by function calls. Here is the example:

[crayon-662891caa7365387671127/]

The call to makeInc creates a binding for x that is referenced inside the function inc. Each call to makeInc creates a new instance of this function, but each instance has a link to a different binding of x. See the output in the Python GUI below:

Simple Example of Closures in Python GUI

Notice that while in a regular closure the enclosed function fully inherits all variables from its enclosing environment, in this construct the enclosed function has only read access to the inherited variables but cannot make assignments to them:

[crayon-662891caa7367753718605/]

Output in Python GUI:

The Enclosed Function has only Read Access to the Inherited Variables but cannot Make Assignments to them

 

3. When do we have Closures in Python?

As seen from the above example, we have a closure in Python when a nested function references a value in its enclosing scope.

The criteria that must be met to create closure in Python are summarized in the following points.

 

4. When to use Closures in Python?

So what are closures good for?

Closures can avoid the use of global values and provides some form of data hiding. It can also provide an object oriented solution to the problem.

When there are few methods (one method in most cases) to be implemented in a class, closures can provide an alternate and more elegant solution. But when the number of attributes and methods get larger, it’s better to implement a class.

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

Exit mobile version