What is the purpose of Python’s __init_subclass__ cls parameter’s keyword arguments, and how are they passed? Python ProfessionalHard Try Now
What does the following code output? import asyncio async def producer(queue): for i in range(3): await queue.put(i) await queue.put(None) async def consumer(queue): while True: item = await queue.get() if item is None: break print(item) async def main(): q = asyncio.Queue() await asyncio.gather(producer(q), consumer(q)) asyncio.run(main()) Python ProfessionalHard Try Now
What is PEP 695 (Python 3.12) type parameter syntax and how does it simplify generic definitions? Python ProfessionalHard Try Now
What does the following pattern with __enter__ returning self enable? class Resource: def __enter__(self): return self def __exit__(self, *args): self.close() return False def close(self): pass with Resource() as r: print(r is not None) Python ProfessionalHard Try Now
What is the purpose of Python’s __set_name__ combined with a class decorator, and in what order are they called? Python ProfessionalHard Try Now
What is the key difference between an iterator and an iterable in Python? Python ProfessionalHard Try Now
What is the purpose of Python’s __class_cell__ mechanism in the context of super() and zero-argument super()? Python ProfessionalHard Try Now
What does the following code output? class Descriptive: def __get__(self, obj, objtype=None): return 42 if obj is None else obj.__dict__.get(‘_val’, 0) def __set__(self, obj, value): obj.__dict__[‘_val’] = value class MyClass: attr = Descriptive() print(MyClass.attr) Python ProfessionalHard Try Now
What does the following code output? from typing import TypeVar, Generic T = TypeVar(‘T’) class Stack(Generic[T]): def __init__(self): self._items: list[T] = [] def push(self, item: T) -> None: self._items.append(item) def pop(self) -> T: return self._items.pop() s: Stack[int] = Stack() s.push(‘not an int’) print(s.pop()) Python ProfessionalHard Try Now
What does the following code output? from itertools import groupby data = [(‘a’, 1), (‘a’, 2), (‘b’, 3), (‘a’, 4)] for key, group in groupby(data, key=lambda x: x[0]): print(key, list(group)) Python ProfessionalHard Try Now
What is the purpose of Python’s __buffer__ protocol (PEP 688, Python 3.12) and the Buffer abstract base class? Python ProfessionalHard Try Now
What is the purpose of __getattr__ versus __getattribute__ in Python? Python ProfessionalHard Try Now
What does the following code output? class MyInt(int): def __new__(cls, value): return super().__new__(cls, value * 2) x = MyInt(5) print(x, type(x)) Python ProfessionalHard Try Now
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) Python ProfessionalHard Try Now
What is the difference between concurrent.futures.ProcessPoolExecutor and ThreadPoolExecutor for CPU-bound tasks? Python ProfessionalHard Try Now
What is the output of the following code? def decorator(cls): original_init = cls.__init__ def new_init(self, *args, **kwargs): original_init(self, *args, **kwargs) self.decorated = True cls.__init__ = new_init return cls @decorator class Foo: def __init__(self): self.value = 1 f = Foo() print(f.value, f.decorated) Python ProfessionalHard Try Now
What is the difference between multiprocessing.Pool.map() and multiprocessing.Pool.imap()? Python ProfessionalHard Try Now