Python decorators: example 4, expanding features with reduce

Let’s make something more useful in this 4th example of usage of python decorators. This time we will not only give more informations about the input, output and function’s operations, but we will add the chance to add or subtract (or easily divide or multiply creating other functions for that), using the decorators.

The code

We have two functions that add and subtracts two numbers.

We want to add or subtract any number without changing anything in the code of the functions but using a decorator.

We use functools reduce

With reduce you can do that:

We do not want to make it in every function that adds, subtracts, divides or multiply… so

Let’s do it with a decorator

This is the output:

Post written by Avatar A python enthusiast

Pygame map editor devlog

The devlog of the Pygame map editor. A work in progress to create a map editor to create maps with tiles with an example of a game that uses these maps. The current version is 1.5 of the map editor and 0.2 of the game, not yet ready for the 1.5 version of the editor (16.9.2020). Repository of the map editor