Python Class Generator
List the attributes of a class and choose how Python should model it: a dataclass, a Pydantic model, a plain class with __init__, __repr__ and __eq__, a NamedTuple or a TypedDict. The generator writes modern type hints, orders attributes so that defaults come last, uses default_factory for lists and dicts, and can add validation and to_dict/from_dict with date conversion.
- Runs in your browser
- No sign-up
- Free to use
How to use Python Class Generator
- Enter the class name and choose the kind of class.
- List the attributes as name: type = default.
- Pick frozen, slots, validation and conversion options.
- Copy the code into a .py file.
Python Class Generator features
Five kinds
dataclass, Pydantic, plain class, NamedTuple, TypedDict.
Modern hints
list[str], dict[str, int] and X | None (Python 3.10+).
Safe defaults
Lists, dicts and “now” never shared between instances.
Validation
__post_init__ or __init__ checks; Pydantic field constraints.
Conversion
to_dict and from_dict with ISO dates.
PEP 8 names
Attribute names converted to snake_case.
When to use Python Class Generator
- Modelling records loaded from JSON, CSV or a database.
- Writing request and response models for FastAPI.
- Creating immutable configuration objects.
- Typing dictionaries passed around in existing code.
Python Class Generator FAQ
Which kind should I choose?
Use a dataclass for internal data, Pydantic when data comes from outside and must be validated, NamedTuple for small immutable records, TypedDict to type existing dictionaries, and a plain class when you need full control.
Why are list defaults written as field(default_factory=list)?
A default like [] would be one list shared by every instance. default_factory creates a new list for each object; dataclasses refuse plain mutable defaults for this reason.
Why were my attributes reordered?
Python does not allow an argument without a default after one with a default, so required attributes are placed first.
What do slots and frozen do?
slots=True saves memory and blocks accidental new attributes; frozen=True makes instances immutable and hashable.
Does Pydantic need extra packages?
Pydantic itself must be installed. EmailStr also needs pydantic[email], which the notes mention when it is used.
Is anything uploaded?
No. The code is generated in your browser.
Choosing a Python class style
Python offers several ways to define a class that mainly holds data, and each has a different purpose. Dataclasses generate __init__, __repr__ and __eq__ from annotated attributes and are the standard choice for internal data. Pydantic models also validate and convert input, which makes them the usual choice at the edges of an application, for example in FastAPI. NamedTuples are immutable and behave like tuples, and TypedDicts describe the keys of ordinary dictionaries for type checkers.
All of them share rules that the generator applies automatically. Attributes without defaults must come before attributes with defaults, so the generator reorders them and tells you. Names are converted to snake_case. Type hints use the modern syntax introduced in Python 3.9 and 3.10, such as list[str] and str | None, and references to other classes are made safe with from __future__ import annotations.
Mutable defaults are a classic Python pitfall: a default list is created once and shared by every instance, so appending to it in one object changes all of them. Dataclasses use field(default_factory=list), plain classes use None and create the list in __init__, and a “now” default calls datetime.now when each object is created rather than when the module is imported.
Validation depends on the style. Dataclasses get a __post_init__ method with checks such as non-empty strings and non-negative amounts, plain classes check in __init__, and Pydantic models use typed fields and constraints such as Field(min_length=8) that report all problems at once.
to_dict and from_dict convert objects to dictionaries ready for json.dumps and back again, writing datetime values as ISO 8601 strings and parsing them with datetime.fromisoformat. Pydantic models already provide model_dump and model_validate, so these methods are not generated for them.