Tool for automated implementation of delegation pattern with ABC
Project description
abc-delegation
A tool for automated delegation with abstract base classes.
This metaclass enables creation of delegating classes inheriting from an abstract base class.
This technique is impossible with regular __getattr__
approach for delegation,
so normally, you would have to define every delegated method explicitly.
Not any more
Basic usage:
from abc import ABCMeta
from abc_delegation import delegation_metaclass
class A(metaclass=ABCMeta):
@abstractmethod
def bar(self):
pass
@abstractmethod
def foo(self):
pass
class B:
def bar(self):
return "B bar"
def foo(self):
return "B foo"
class C(A, metaclass=delegation_metaclass("my_delegate")):
def __init__(self, b):
self.my_delegate = b
def foo(self):
return "C foo"
c = C(B())
assert c.foo() == "C foo"
assert c.bar() == "B bar"
Multiple delegates:
from abc import ABCMeta
from abc_delegation import multi_delegation_metaclass
class A(metaclass=ABCMeta):
@abstractmethod
def bar(self):
pass
@abstractmethod
def foo(self):
pass
@abstractmethod
def baz(self):
pass
class B:
def bar(self):
return "B bar"
def foo(self):
return "B foo"
class X:
def baz(self):
return "X baz"
class C(A, metaclass=multi_delegation_metaclass("_delegate1", "_delegate2")):
def __init__(self, d1, d2):
self._delegate1 = d1
self._delegate2 = d2
def foo(self):
return "C foo"
c = C(B(), X())
assert c.bar() == "B bar"
assert c.foo() == "C foo"
assert c.baz() == "X baz"
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
abc_delegation-0.1.tar.gz
(2.5 kB
view hashes)
Built Distributions
abc_delegation-0.1-py3.7.egg
(4.1 kB
view hashes)
Close
Hashes for abc_delegation-0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1a17568f30aef7ce71bee15f37c6d5802a101d2c2c99f519cee7004ce0c64f26 |
|
MD5 | 0aa6e55d9d8ed754116c7cfa2618a824 |
|
BLAKE2b-256 | 7a2b33cdec125ec83263d27296e04276234c697d651fa342106cd3dcdecc5879 |
Close
Hashes for abc_delegation-0.1-py2-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | bf5d337b83b476a3a30217500bfd4ed5d0e29fa235e0931b1af9fbb6020d5e71 |
|
MD5 | 62ab6b29adb415d2af2dd3f00564a6ab |
|
BLAKE2b-256 | 589153f5a3d1abd9c55921cef5cbbc9c4ddc21cab7f6982f8642bf53e5bfa30b |