Python Professional — Hard
Ready to go further?
Related questions
- 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 difference between threading.Lock and threading.RLock in Python?
- What does the following descriptor code output? class Validator: def __set_name__(self, owner, name): self.name = name def __get__(self, obj, objtype=None): if obj is None: return self return obj.__dict__.get(self.name) def __set__(self, obj, value): if not isinstance(value, int): raise TypeError(f'{self.name} must be int') obj.__dict__[self.name] = value class Point: x = Validator() p = Point() p.x = 5 print(p.x)
- What does the following code output? import sys class MyList(list): def __sizeof__(self): return super().__sizeof__() + 100 ml = MyList([1, 2, 3]) print(sys.getsizeof(ml) - sys.getsizeof([1, 2, 3]))
- What does the following code output? from dataclasses import dataclass, field @dataclass class Config: tags: list = field(default_factory=list) c1 = Config() c2 = Config() c1.tags.append('x') print(c2.tags)
- Which of the following correctly explains how Python's asyncio event loop executes coroutines?
