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 is the purpose of __set_name__ in Python descriptors?
- What is the purpose of Python's __init_subclass__ cls parameter's keyword arguments, and how are they passed?
- Which of the following best describes a Python descriptor?
- What is the output of the following code? class Singleton: _instance = None def __new__(cls, *args, **kwargs): if cls._instance is None: cls._instance = super().__new__(cls) return cls._instance a = Singleton() b = Singleton() print(a is b, id(a) == id(b))
- What is the purpose of Python's __buffer__ protocol (PEP 688, Python 3.12) and the Buffer abstract base class?
- What is the output of the following code involving __missing__? class DefaultList(dict): def __missing__(self, key): self[key] = [] return self[key] d = DefaultList() d['a'].append(1) d['a'].append(2) d['b'].append(3) print(d)
