[Python] How to compare objects based on their superclass

Discussão em 'Python' iniciado por Stack, Março 18, 2025.

  1. Stack

    Stack Membro Participativo

    How do I check for equality using super-class-level comparison?

    from dataclasses import dataclass

    @dataclass(order=True)
    class Base:
    foo: int

    @dataclass(order=True)
    class X(Base):
    x: str

    @dataclass(order=True)
    class Y(Base):
    y: float

    x = X(0, "x")
    y = Y(1, 1.0)


    How do I compare x and y on their Base attributes only?

    Base.__eq__(x,y) seems to do the job, is this the "pythonic" way?

    Continue reading...

Compartilhe esta Página