What is the purpose of Python’s __reduce__ and __reduce_ex__ methods? 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 is the difference between threading.Lock and threading.RLock in Python? 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 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 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
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 code using __slots__ with inheritance? Python ProfessionalHard Try Now
What is the difference between @staticmethod and a module-level function, and when should you prefer one over the other? Python ProfessionalHard Try Now
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) Python ProfessionalHard Try Now
What is ExceptionGroup in Python 3.11+ and how does except* differ from except? Python ProfessionalHard Try Now
What does the following code output? def make_counter(): count = 0 def increment(n=1): nonlocal count count += n return count def reset(): nonlocal count count = 0 increment.reset = reset return increment c = make_counter() c(3) c(2) c.reset() print(c()) Python ProfessionalHard Try Now
What is the output of the following code? from contextlib import contextmanager @contextmanager def managed(): print(‘enter’) try: yield 42 finally: print(‘exit’) with managed() as v: print(v) Python ProfessionalHard Try Now
What does the following code output? from contextlib import contextmanager @contextmanager def managed(): print(‘enter’) try: yield 42 finally: print(‘exit’) with managed() as v: print(v) Python ProfessionalHard Try Now
What is the correct way to implement a thread-safe singleton in Python using a lock? Python ProfessionalHard Try Now