CompclassMeta (metaclass)¶
CompclassMeta
¶
Bases: ABCMeta
Metaclass that adds class attributes/methods from delegates
to clsname
object as class properties.
Usage:
class Foo:
a = 1
def __len__(self) -> int:
# custom len method
return 42
class Bar(metacls=CompclassMeta, delegates={"_foo": ("a", "__len__")}):
def __init__(self, foo: Foo):
self._foo = foo
foo = Foo()
bar = Bar(foo)
bar.a # -> 1 (instead of bar._foo.a)
len(bar) # -> 42 (instead of len(bar._foo))
Source code in compclasses/_meta.py
__new__(clsname, bases, attrs, delegates, verbose=True, log_func=logger.info)
¶
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cls |
Type
|
Metaclass |
required |
clsname |
str
|
Class name |
required |
bases |
Tuple[Type, ...]
|
Base classes |
required |
attrs |
Dict[str, Any]
|
Class attributes |
required |
delegates |
Dict[str, Union[Iterable[str], delegatee]]
|
Key-value pair of delegates.
|
required |
verbose |
bool
|
Defines the level of verbosity when setting those forwarded methods. |
True
|
log_func |
Callable[[str], None]
|
Function to use for logging, if verbose is set to True. |
logger.info
|