Core Python Internals
1. What does the GIL prevent, and when do threads still help?
The Global Interpreter Lock lets only one thread run Python bytecode at a time in the standard CPython build. Threads therefore do not speed up CPU-bound Python code.
They still help with I/O. A thread that waits on a socket or a file releases the GIL. Other threads run in the meantime. Many C extensions, such as NumPy and hashlib on large inputs, also release it during heavy work.
The exception to all of this is the free-threaded build, covered in question 26.
2. How does CPython free memory?
Mostly through reference counting: every object counts the references to it and is freed the moment the count reaches zero. That makes cleanup predictable: in CPython, a file object is closed as soon as its last reference goes away.
Reference counting cannot free cycles, such as two objects that point to each other. A cyclic garbage collector runs now and then to find them.
One detail worth knowing: Python 3.14.0 to 3.14.4 shipped an incremental collector with shorter pauses. 3.14.5 reverted it to the 3.13 collector after reports of memory pressure in production.
3. What is a descriptor, and where do you use one without noticing?
A descriptor is an object that defines __get__, __set__ or __delete__ and controls attribute access when it is stored on a class. property, classmethod, staticmethod and ordinary methods are all descriptors: a function's __get__ is what binds self.
ORMs use them too. A Django foreign key attribute is a descriptor that runs a query the first time you read it. That is how N+1 problems start. The Descriptor HowTo Guide is the reference.
4. When would you use a metaclass instead of __init_subclass__?
Rarely. __init_subclass__ runs whenever a class is subclassed and covers the common metaclass use cases: registering plugins, validating class attributes and setting defaults.
class Handler:
registry: dict[str, type["Handler"]] = {}
def __init_subclass__(cls, *, event: str, **kwargs):
super().__init_subclass__(**kwargs)
Handler.registry[event] = cls
class PaymentFailed(Handler, event="payment.failed"):
...
A metaclass is still needed to change how the class object itself is built or behaves. Examples are a custom __prepare__ or a class that is itself iterable. Django's ModelBase is a metaclass for that reason.
Two metaclasses in one hierarchy also conflict, which is another reason to avoid them.
5. What do __slots__ buy you, and what do they cost?
__slots__ replaces the per-instance __dict__ with fixed storage for the named attributes. That saves memory when a process holds millions of small objects, and it makes typos in attribute names raise AttributeError.
The cost: no attributes beyond the listed ones, no __dict__ unless you list it, and care with multiple inheritance. Dataclasses can generate them with @dataclass(slots=True).
6. Protocol or abstract base class for a service interface?
A typing.Protocol describes a shape that any class matches without inheriting from it; an ABC requires inheritance (or explicit registration) and can hold shared code.
For dependency injection in a backend, a Protocol is usually the better fit.
from typing import Protocol
class PaymentGateway(Protocol):
def charge(self, customer_id: str, cents: int) -> str: ...
def checkout(gateway: PaymentGateway, customer_id: str, cents: int) -> str:
return gateway.charge(customer_id, cents)
A test fake or a third-party client matches the Protocol without importing your code. Type checkers verify the match; at runtime nothing is checked unless you add @runtime_checkable.
7. How do you stream a large export without loading it into memory?
Use a generator that yields rows or chunks, and a response type that consumes an iterator. Memory then stays flat whatever the export size.
import csv, io
from django.http import StreamingHttpResponse
def rows(queryset):
buf = io.StringIO()
writer = csv.writer(buf)
for order in queryset.iterator(chunk_size=2000):
writer.writerow([order.id, order.total])
yield buf.getvalue()
buf.seek(0)
buf.truncate(0)
def export(request):
return StreamingHttpResponse(rows(Order.objects.all()), content_type="text/csv")
QuerySet.iterator() matters as much as the generator: without it Django caches every fetched row on the queryset. FastAPI and Starlette offer the same pattern with StreamingResponse.