libb.delegate

delegate(deleg, attrs)[source]

Delegate attribute access to another object.

Creates properties that forward attribute access to a specified delegate object, enabling composition over inheritance.

Parameters:
  • deleg (str) – Name of the attribute containing the delegate object.

  • attrs (str or list[str]) – Single attribute name or list of attribute names to delegate.

Return type:

None

Delegate Simple Attributes:

>>> class X:
...     a = 1
>>> class Y:
...     x = X()
...     delegate('x', 'a')
>>> Y().a
1

Delegate Methods:

>>> class A:
...     def echo(self, x):
...         print(x)
>>> class B:
...     a = A()
...     delegate('a', ['echo'])
>>> B().echo('whoa!')
whoa!