June 2019
Beginner to intermediate
770 pages
19h 24m
English
As we noted in Chapter 9, Decorators and Mixins – Cross-Cutting Aspects, creating a class-level logger can be done with a decorator. This will separate logger creation from the rest of the class. A common decorator idea that is very simple has a hidden problem. Here's the example decorator:
def logged(cls: Type) -> Type: cls.logger = logging.getLogger(cls.__qualname__) return cls
The @logged decorator creates the logger attribute as a feature of a class. This can then be shared by all of the instances. With this decorator, we can define a class with code like the following example:
@loggedclass Player_1: def __init__(self, bet: str, strategy: str, stake: int) -> None: self.logger.debug("init bet %s, strategy ...
Read now
Unlock full access