__class__ equality check from two different imports

__class__ equality check from two different imports



I have a rather complicated import setup in my python software project where I don't get around importing some modules dynamically with importlib.
Therefore I run into situations where I have two objects of the same class, but since they have been loaded independently the equality check between them gives me False.


False



Their string representations of each object's obj.__class__ look the same:
<class 'drivers.MongoDriver'> vs <class 'drivers.MongoDriver'>.
Equality checks with __eq__ on both classes, issubclass, __eq__ on type() and isinstance fail, though.


obj.__class__


<class 'drivers.MongoDriver'>


<class 'drivers.MongoDriver'>


__eq__


issubclass


__eq__


type()


isinstance



I could think of a hacky way to still do a equality check by comparing repr(obj.__class__), but that doesn't really seem so nice.
Another solution would be to "hide" an attribute specifying some unique class id.


repr(obj.__class__)



Is there another (better) way how to do equality checks between two classes that are essentially the same but have been loaded by importlib independently?


importlib



Edit:
Important: I'm not looking for a way to avoid importing the class twice.
Some modules of my project are reimported on purpose dynamically to account for code changes in those modules during runtime.
Those modules then again import other modules, such as the driver I want to make an equality check with. And even though it would be nice to not have to reimport those non-changing modules that are used by the changing modules, I am aware that whenever I dynamically import one module that all dependencies are then reimported as well. (If you have a solution on how to reimport a module A which itself has a (static) import B, but not have to reimport B together with A, I would be happy. But this is not directly what I'm asking/looking for.)





How about not loading the same module twice to begin with? Solve the X instead of the Y.
– Aran-Fey
Sep 1 at 13:18






where I don't get around importing some modules dynamically with importlib, please read my post properly
– h345k34cr
Sep 1 at 13:21






Well, those classes are not really the same class, exactly because you imported them separately. You could, for instance, do drivers.MongoDriver = decorate(drivers.MongoDriver) and it would modify only one of them. You should really avoid cause rather than the consequences.
– zvone
Sep 1 at 13:21


drivers.MongoDriver = decorate(drivers.MongoDriver)





BTW, you did not really explain how it happens that your class is imported twice, so we cannot help you avoid the problem.
– zvone
Sep 1 at 13:22





They are imported twice because some modules are reimported on purpose (on code changes). Not the drivers themselves, but the modules that are reimported import those drivers. I am fully aware that they really are not the same classes, I'm rather looking for a workaround since I can't change much on the dynamic imports. Those are simply an intended part of the software
– h345k34cr
Sep 1 at 13:26







Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Some of your past answers have not been well-received, and you're in danger of being blocked from answering.



Please pay close attention to the following guidance:



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)