Skip to main content

Test Coverage Stable Version Pre-release Version PyPI - Python Version PyPI Downloads

delegate-pattern: Python implementation of the Delegation Pattern.

delegate-pattern provides a basic implementation of the well-known Delegation Pattern.

What is delegation

Delegation is a pattern in object oriented programming where a class (delegator) delegates responsibilities to one or more delegates.

This allows for greater code reusability and reduced class complexity and may help adhering to the DRY (Do not Repeat Yourself) and SoC (Separation of Concerns) principles.

Example

Consider a trivial task of delegating the printing of an objects name to the console. Here the delegator SomeClass delegates the task to the delegate PrintNameDelegate which is not much more than a function wrapped in a class.

The delegate class is always initialized with an argument specifying its delegator, and while this example shows the use of a protocol class called NamedClassProtocol, this is merely included for convenience, and will not be enforced at runtime. In fact the type of delegator can be anything, but delegate constructors cannot have any other arguments.

Note that it's strongly recommended to use weak references to the delegator in the delegate constructor.

from typing import Protocol
from weakref import ref
from delegate.pattern import delegate

class NamedClassProtocol(Protocol):
    _name: str

class PrintNameDelegate:
    # this is a stateful delegate because its constructor
    # takes a 'delegator' argument

    def __init__(self, delegator: NamedClassProtocol):
        self.__delegator = delegator

    def __call__(self):
        print(self.__delegator._name)

class NamePropertyDelegate:
    # this is a stateless delegate because it has no
    # constructor which takes a 'delegator' argument

    def __get__(self, delegator: NamedClassProtocol) -> str:
        return delegator._name

    def __set__(self, delegator: NamedClassProtocol, value: str):
        delegator._name = value

class SomeClass:
    _name: str
    def __init__(self, name: str) -> None:
        self._name = name

    name_printer = delegate(PrintNameDelegate) # => PrintNameDelegate instance
    name = delegate(NamePropertyDelegate, str) # => string getter

some_instance = SomeClass("Neo")
some_instance.name_printer() # prints Neo

name = some_instance.name # => Neo
some_instance.name = "Trinity"
new_name = some_instance.name # => Trinity

Full documentation

Go to documentation

Metadata

Release files for delegate-pattern 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for delegate-pattern 0.1.0
File Size Uploaded
delegate_pattern-0.1.0.tar.gz 9.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for delegate-pattern 0.1.0
File Interpreter ABI Platform
delegate_pattern-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 20.8 kB

Release files / delegate_pattern-0.1.0.tar.gz

Download URL delegate_pattern-0.1.0.tar.gz
Size 9.8 kB
Tags Source
SHA-256 checksum
How to use checksums
c8deeceaff9715d61a302ab2f7a6dd8bc75f1967ebe16fb0ead4560652dc8fea
BLAKE2b-256 checksum
How to use checksums
08cd3c42ae55165c59446dca5c358d29cc4c9a85833ffa3da95b77f6309625b9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 27, 2025.

Transparency log

Release files / delegate_pattern-0.1.0-py3-none-any.whl

Download URL delegate_pattern-0.1.0-py3-none-any.whl
Size 11.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
624058a99072893b006c467d947c5ccb23ebfc3dc1098d54536059fdabbd9524
BLAKE2b-256 checksum
How to use checksums
5b1fb6b9f2586410f51449b1ba5572b468e6bb09ef2ec7de0f4909d9bb4b689d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 27, 2025.

Transparency log

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

0.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page