Skip to content
Learn Netverks

Lesson

Step 21/36 58% through track

dataclasses-intro

Dataclasses intro

Last reviewed May 28, 2026 Content v20260528
Track mode
server_script
Means
Server runner
Reading
~1 min
Level
intermediate

This lesson

An orientation to the Python track—how the compiled playground works, core vocabulary, and what you will practice next.

You need a clear map of the Python track so indentation, mutability, imports, and the stdlib do not feel like magic.

You will apply Dataclasses intro in contexts like: Scripts, Django/FastAPI apps, notebooks, and glue code between systems.

Write Python 3 in the editor and click Run on server—the dev runner executes your script with print() for output; stdlib only in playground snippets (LEARNING_RUNNER_ENABLED=true). Also read the interview prep blocks.

Excellent first language—finish JavaScript basics if you already know the web, then Django for server-rendered apps.

dataclasses (stdlib) auto-generate __init__, __repr__, and comparison methods for data-focused types—similar to C# record or Java records for plain data carriers.

Basic dataclass

from dataclasses import dataclass

@dataclass
class Product:
    sku: str
    price: float
    qty: int = 0

frozen and field()

from dataclasses import dataclass, field

@dataclass(frozen=True)
class Config:
    host: str
    tags: list[str] = field(default_factory=list)

Use default_factory for mutable defaults—same rule as function defaults. frozen=True makes instances immutable.

Important interview questions and answers

  1. Q: dataclass vs namedtuple?
    A: Dataclasses are mutable by default, support defaults and methods; namedtuple is tuple subclass—immutable and lighter.
  2. Q: Type annotations required?
    A: Fields need annotations for dataclass generation—also documents shape for type checkers.

Self-check

  1. What decorator marks a dataclass?
  2. How provide a default empty list safely?

Tip: Use field(default_factory=list) for mutable defaults—same rule as function defaults.

Interview prep

dataclass vs regular class?

Auto-generates __init__, __repr__, comparisons—less boilerplate for data carriers.

default_factory why?

Avoids shared mutable defaults on dataclass fields—same pitfall as function defaults.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • dataclass vs dict?
  • frozen=True?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump