Python for Delphi (P4D) is a set of free components that wrap up the Python DLL into Delphi, maXbox and Lazarus (FPC). They let you easily execute Python scripts, create new Python modules and new Python types. You can create Python extensions as DLLs and much more. P4D supports als maXbox functionality in a runtime script, which we will show in 10 programming concepts:
const pyCLosure =
'def make_multiplier(x): '+lf+
' def multiply(y): '+lf+
' return x * y '+lf+
' return multiply ';
const pyDeco =
'def logger(func): '+lf+
' def wrapper(*args, **kwargs): '+lf+
' print(f"Calling {func.__name__}") '+lf+
' return func(*args, **kwargs) '+lf+
' return wrapper ';
function PyCode_10_ProgrammingConcepts(my_project: string): string;
begin
with TPythonEngine.Create(Nil) do begin
try
loadDLL;
autofinalize:= false;
ExecString('import io,sys');
//execString('import comtypes.client');
execString('from contextlib import contextmanager');
//execStr('from freegames import floor, square, vector');
execstr('output = io.StringIO()') //for print out
execstr('sys.stdout = output')
try
//1. Closures
execstr(pyCLosure);
ExecStr('double = make_multiplier(2)'+lf+
'print(double(5)) # ➜ 10 ');// # ➜ 10');
//2. Decorators = Metaprogramming With Manners
execstr(pyDEco);
ExecStr('@logger'+lf+
'def greet(name): '+lf+//
' return f"Hello, {name}"'+lf+
'greet("Ahad")');
//3. Lambda Isn’t for Show-Offs
ExecStr('numbers = [1, 2, 3, 4]'+lf+
'squares = list(map(lambda x: x**2, numbers))'+lf+//# ➜ 1,4,9,16
'print(squares)');
//4. Generators Are Lazy — And That’s Why They Win
ExecStr('def countdown(n): '+lf+
' while n > 0: '+lf+
' yield n '+lf+
' n -= 1 '+lf+
' '+lf+
'for num in countdown(5):'+lf+
' print(num)');
//5. Context Managers: Automation for Setup + Cleanup
ExecStr('with open("pylog.txt", "w") as f:'+lf+
' f.write("Hello box, file!") # ➜ no close ');
//6. Currying Isn’t Fancy — It’s Just Pre-Filled Functions
execString('from functools import partial');
ExecStr('def power(base, exponent): '+lf+
' return base ** exponent '+lf+//
'square = partial(power, exponent=2)'+lf+
'print("square:",square(5)) # ➜ 25');
//7. Memoization: Because Redoing Work is for Amateurs
execString('from functools import lru_cache');
ExecStr('@lru_cache '+lf+
'def fib(n): '+lf+
' if n < 2: '+lf+
' return n '+lf+
' return fib(n-1) + fib(n-2) '+lf+
' '+lf+
'print(fib(50)) #Fast for big inputs');
//8. Comprehensions: The Swiss Army Knife of Python Automation
execStr('squares = [x**2 for x in range(10) if x % 2 == 0]'+lf+
'print("comprehension: ",squares)');
//9. Exception Handling Is About Controlling the Unexpected — Automatically
ExecStr('try: '+lf+
' result = 10 / 0 '+lf+
'except ZeroDivisionError: '+lf+
' result = 0 '+lf+
'finally: '+lf+
' print(f"Result is {result}")');
//10. The Magic of __call__: Your Class Can Act Like a Function
ExecStr('class Greeter: '+lf+
' def __call__(self, name): '+lf+
' return f"Hello, {name}" '+lf+
' '+lf+
'greet = Greeter() '+lf+
'print(greet("Ahad")) # ➜ Hello, Ahad');
print('VTAG: '+evalstr('sys.version'));//+
(* ExecStr('speak = comtypes.client.CreateObject("SAPI.SpVoice")'+lf+
'filestream = comtypes.client.CreateObject("SAPI.spFileStream")'+lf+
'filestream.open(r"'+exepath+'ccout.wav", 3, False)'+lf+
'speak.AudioOutputStream = filestream'+lf+
'speak.Speak("this is maxbox 5 test")'+lf+
'filestream.close()'); *)
println('out: '+stringreplace(evalstr('output.getvalue().strip()'),
#10,#13#10,[rfReplaceAll, rfIgnoreCase]));
result:= ('All 10 Done:'+(EvalStr('sys.version')));
except
writeln(ExceptionToString(ExceptionType, ExceptionParam));
end;
except
raiseError;
finally
free;
end;
end;
end;
P4D makes it very easy to use python as a scripting language for Delphi applications. It also comes with an extensive range of demos and tutorials.
Then the output of our 10 concepts will be:
VTAG: 3.14.3 (tags/v3.14.3:323c59a, Feb 3 2026, 16:04:56) [MSC v.1944 64 bit (AMD64)]
out: 10
Calling greet
[1, 4, 9, 16]
5
4
3
2
1
square: 25
12586269025
comprehension: [0, 4, 16, 36, 64]
Result is 0
Hello, Ahad
All 10 Done:3.14.3 (tags/v3.14.3:323c59a, Feb 3 2026, 16:04:56) [MSC v.1944 64 bit (AMD64)]
mX5🐞 executed: 23/09/2026 17:16:25 Runtime: 0:0:3.412 Memload: 78% use
RemObjects Pascal Script. Copyright (c) 2004-2026 by RemObjects Software & maXbox5

Top comments (0)