Skip to content Skip to sidebar Skip to footer

What Are The Measures To Call A Python Code A Policy-based Design?

Description I wonder if the code I am showing can be considered as an example of policy-based design in Python. Also, I would like to know if have you seen python modules using som

Solution 1:

The answer is the product of what I learn based on my research and some comments from StackOverflow.

I wrote a detail blog about these approaches called Mixin and policy-based design in python. In that post, I also discuss several extensions like implementations using decorators, adding default classes with mixins, and more ...

Design requirements and implementations

Requirement

Design a Python module such users can create classes with new behavior and interfaces by inheriting from a predefined set of classes. This approach provides a large number of classes available to the users based on all possible ways they can be combined into new ones.

This requirement can be implemented using mixin and policy-based implementations.

Mixin implementation

Mixin implementation is defined as follow:

  • Module classes are divided between mixin and base classes.
  • Mixin classes implement modifications to the behavior or interface of a base class.
  • Users create a new class by combining one or more mixin(s) with base classes by inheritance.
classMixin1Class:
    """Implementation of some Mixin1 class."""classMixin2Class:
    """Implementation of some Mixin2 class."""
...
classBasedClass:
    """Implementation of based class."""# End user creating new classclassNewClass(Mixin1Class, [Mixin2Class, ...], BasedClass):
    pass

References:

Policy-based implementation

Policy-based implementation is defined as follow:

  • Module classes are divided between policy and host classes.
  • Policy classes implement modifications to the behavior or interface of the host.
  • Host classes are defined withing class factories i.e., a function that returns type objects.
  • Users invoke the creation of new class using class factories.
  • Policy classes are passed as arguments to the class factory.
  • Withing the class factory, the host class is created with all the policy classes as its parents.
classPolicy1Class:
    """Implementation of some Policy1 class."""classPolicy2Class:
    """Implementation of some Policy2 class."""
...
defHostClassFactory(Policy1, Policy2=Policy2Class, ...):
    """Create a HostClass and return it."""classHostClass(Policy1, Policy2, ...):
        """Concrete implementation of the host class."""return HostClass

# End user invoking new class
NewClass = HostClassFactory(Policy1Class, [Policy2Class,...])

References:

Comparison between implementations

Here a list of the differences between approaches:

  • The relationship between classes is not the same affecting the Python's Method Resolution Order or MRO, see figure below.
  • The mixin's base classes are defined or instantiated when the user creates the new class.
  • However, policy-based host class definition is delayed until the user calls the factory function.
  • It is possible to provide default policy classes, but you cannot have mixin default classes.

Post a Comment for "What Are The Measures To Call A Python Code A Policy-based Design?"