[Python] objects of class defined in different modules may have different type

Discussão em 'Python' iniciado por Stack, Maio 26, 2025.

  1. Stack

    Stack Membro Participativo

    I define a dataclass MyClass in module A and create two instances of it, a in module A and b in module B. When comparing the two, it always returns False, since a has type <class '__main__.MyClass'> and b has type <class 'A.MyClass'>.

    Is this expected behaviour (python version is 3.13.2)?

    If yes, what would be a good way to compare two instances of the same dataclass when one is not sure type returns the same for both instances?

    If no, should this be fixed in dataclasses or is this a python issue?

    MWE without the dataclasses, just a raw class:

    module A:

    import B

    class MyClass:
    pass

    if __name__ == '__main__':
    a=MyClass()
    b=B.b()

    print(type(a))
    print(type(b))


    module B:

    import A

    def b():
    return A.MyClass()


    When running A.py, the output is

    <class '__main__.MyClass'>
    <class 'A.MyClass'>

    Continue reading...

Compartilhe esta Página