Decorator Unification Protocol

The most general decoration variant from Pyc. 6 allows for arbitrary dynamic decorations anywhere in the code. But neither are all of these options needed for every decoration, nor is it generally useful to perform decorations anywhere in the code. Mostly a static decoration like in Pyc. 4 is sufficient and also provides a better code quality, because by decorating directly on the original class or function its modification becomes more visible.

Comment:

If dynamic decoration is needed, a compact variant as in Pyc. 3 is recommended to keep definition and decoration of an object as close together as possible.

With this in mind, a Decorator Unification Protocol is formulated to unify three requirements:

Decorator Unification Protocol

  1. enable the most general dynamic decoration Pyc. 6, and

  2. support the most common static decoration Pyc. 4, but

  3. also allows unparameterized decorations as in Pyc. 2.

The first requirement simply states that the implementation must support the most general variant with all parameters, that is, calls of the form

Decorator(*deco_args, **deco_kwargs)(substitute, *args, **kwargs)

just as in Pyc. 6, where the substitute is the original function or class to be modified.

The second requirement refers to the implementation of the most general static decoration in pie notation as in Pyc. 4 which is implemented by the Python compiler as shown in Pyc. 5. For this to work together with the first requirement,

all extra parameters *args and **kwargs must be defined with default values

or can of course be omitted if they are not needed.

The third requirement considers the situation when no decorator parameters at all are present. Then the question arises whether an empty pair of brackets must be specified for the first call level in static decoration, or whether it can simply be omitted. Semantically, the two decorations @Decorator() def substitute (with empty brackets) and @Decorator def substitute (without brackets) express absolutely identical facts,

@Decorator() def substitute  ==  @Decorator def substitute

and so both decorations should be possible on the one hand and therefore lead to the same result on the other hand. It should be left to personal taste which of these two equivalent variants is chosen.


◄ prev

up

next ►

Legal Notice

Privacy Policy

Cookie Consent

Sphinx 7.2.6 & Alabaster 0.7.12

© Copyright 2020-, Martin Abel, eVation. All Rights Reserved.