Python Professional — Hard
Explanation
A decorator is a design pattern in Python that allows a user to add new functionality to an existing object without modifying its structure. Decorators are usually called before the function they decorate.
Ready to go further?
Related questions
- What does `__mro__` stand for and where is it used?
- What is the output of the following code? class Chainable: def __init__(self, value): self.value = value def add(self, n): self.value += n return self def mul(self, n): self.value *= n return self result = Chainable(2).add(3).mul(4).value print(result)
- What does the following code output, and what pattern does it demonstrate? class Registry: _registry = {} def __init_subclass__(cls, key=None, **kwargs): super().__init_subclass__(**kwargs) if key: Registry._registry[key] = cls class Dog(Registry, key='dog'): pass class Cat(Registry, key='cat'): pass print(Registry._registry)
- What is the output of `bool([] or {} or 0 or 'hello')`?
- What is the purpose of __fspath__ and os.fspath() in Python?
- What is the result of `isinstance(True, int)` in Python?
