Python Developer Interview Question

When would you choose a dataclass over a NamedTuple or a Pydantic model?

What the interviewer is probing, how to structure your answer, and a spoken example you can adapt.

Quick answer

Use a dataclass for internal mutable structures where you want ordinary attributes and free init, repr, and eq. Use a NamedTuple when you want an immutable, tuple like record that unpacks and hashes cheaply. Use Pydantic at system boundaries, where you need runtime validation, coercion, and serialization of data arriving from HTTP, config files, or queues.

Why interviewers ask this

There are four or five ways to declare a record in modern Python, and the interviewer wants to know you pick deliberately instead of reaching for whatever you used last. The key insight they listen for is that type hints on a dataclass are not enforced at runtime, so validation is exactly what Pydantic buys you, and that paying for validation on every internal object is wasted work.

How to structure your answer

  • Sort the three by where the data comes from.
  • Say dataclass hints are not enforced at runtime.
  • Give the immutability and hashing angle for NamedTuple.
  • State your default and the boundary rule.

Example answer

Spoken example, first person

My rule is that Pydantic guards the edges and dataclasses live inside. Anything crossing a boundary, a request body, an environment config, a message off a queue, gets a Pydantic model because I want it validated and coerced once, loudly, at the point it arrives. Everything downstream of that can be a plain dataclass with slots turned on, since I already know the data is good and I do not want to pay validation cost on every object I construct in a loop. The thing people miss is that annotating a dataclass field as int does nothing at runtime, so pass a string and it stores the string happily. NamedTuple I use less, mainly for small immutable records that need to be dict keys or get unpacked, like a coordinate pair or a cache key. On a service I worked on, switching an internal hot path from Pydantic models to slotted dataclasses cut a batch job runtime noticeably, purely from skipping revalidation of data we had already checked.

Walking into this interview soon? GhostPilot listens to your live call, spots the question the moment it is asked, and puts a structured answer on your screen in real time. Try it on your next mock, or grab a $29 Session Pass, no subscription, for the real thing.

See how it works

Follow-up questions to expect

  • What does slots equals True do on a dataclass?
  • How do you make a dataclass immutable?
  • What changed between Pydantic v1 and v2?

Related python developer questions

Your interviewer will ask their own version of this. Paste your actual job description into the free Question Predictor and get the 20 questions that role is most likely to ask, with what each one is really probing.

Predict my questions

Rehearse the hard questions before they are asked

Practise with a live copilot, then walk in ready. A $29 Session Pass gets you through the interview with no subscription and no lock-in.

Get GhostPilot