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 is the purpose of Python’s __buffer__ protocol (PEP 688, Python 3.12) and the Buffer abstract base class? 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 __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 is the difference between concurrent.futures.ProcessPoolExecutor and ThreadPoolExecutor for CPU-bound tasks? 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 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 purpose of Python’s __reduce__ and __reduce_ex__ methods? Python ProfessionalHard Try Now
What is the difference between multiprocessing.Pool.map() and multiprocessing.Pool.imap()? Python ProfessionalHard Try Now
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) Python ProfessionalHard Try Now
What does the following code output? from abc import ABC, abstractmethod class Animal(ABC): @abstractmethod def speak(self): … def describe(self): return f’I say {self.speak()}’ class Dog(Animal): def speak(self): return ‘woof’ print(Dog().describe()) Python ProfessionalHard Try Now
What is the difference between threading.Lock and threading.RLock in Python? Python ProfessionalHard Try Now
What does the following async code output? import asyncio async def task(n): await asyncio.sleep(0) return n * 2 async def main(): results = await asyncio.gather(task(1), task(2), task(3)) print(results) asyncio.run(main()) Python ProfessionalHard Try Now
What does the following code demonstrate about Python’s name mangling? class Secure: def __init__(self): self.__secret = 42 s = Secure() print(s.__secret) Python ProfessionalHard Try Now
What is the output of the following? import weakref class Obj: pass obj = Obj() ref = weakref.ref(obj) print(ref() is obj) del obj print(ref()) Python ProfessionalHard Try Now