[Python] How to use the data class itself as the type of its attribute [duplicate]

Discussão em 'Python' iniciado por Stack, Agosto 29, 2025.

  1. Stack

    Stack Membro Participativo

    I have a data class:


    from dataclasses import dataclass
    from typing import Any


    @dataclass
    class Url:
    http: bool
    params: dict[str: Any]
    body: str

    # More stuff here


    Here I have a Url data class with three attributes, http which is a boolean, params which is a dictionary with a string key and a value of type Any, and body, which is the URL string (i.e. google.com).

    I want to add a new attribute named linked which should be another Url dataclass. So, I tried adding this:

    linked: Url


    but this gave me an error:

    NameError: name 'Url' is not defined


    Is this even possible? If so, how can I achieve do this?

    Continue reading...

Compartilhe esta Página