lazr.delegates

2.1.1 · active · verified Fri Apr 17

lazr.delegates provides a simple, declarative way to create Python objects that delegate behavior to other objects. It simplifies the implementation of common design patterns like composition, allowing classes to expose methods from 'delegatee' objects without boilerplate code. The current version is 2.1.1, and the library maintains a stable, albeit infrequent, release cadence, primarily focusing on Python compatibility and bug fixes.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates how to use the `@delegates` decorator to easily expose methods from another class. `MyPet` delegates `make_sound` and `get_animal_type` to an instance of `SoundMaker` implicitly.

from lazr.delegates import delegates

class SoundMaker:
    def make_sound(self):
        return "Woof!"

    def get_animal_type(self):
        return "Dog"

@delegates(SoundMaker)
class MyPet:
    # MyPet now delegates make_sound and get_animal_type to SoundMaker
    pass

pet = MyPet()
print(f"Pet sound: {pet.make_sound()}")
print(f"Pet type: {pet.get_animal_type()}")

view raw JSON →