0

I'm trying to find out whether two enum instances represent the same item of an enum class. Imagine the following simple program which works as expected:

from enum import Enum, auto

class myEnum(Enum):
    ITEM1 = auto()
    ITEM2 = auto()

enum1 = myEnum.ITEM1
enum2 = myEnum.ITEM2
enum3 = myEnum.ITEM1

print(enum1 == enum2)
print(enum1 == enum3)

print(enum1 == myEnum.ITEM1)
print(enum1 == myEnum.ITEM2)

Output:

False
True
True
False

My real application is slightly more complicated with creation of enum instances at different places, but all of the same class defined in a single module. Here the result is not as expected, even if the instances reflect the same value:

itype
<InstrumentTypes.FIXEDPOINTCELL: 5>

type(itype)
<enum 'InstrumentTypes'>

a
<InstrumentTypes.FIXEDPOINTCELL: 5>

type(a)
<enum 'InstrumentTypes'>

a == itype
False

Regarding the comments below it doesn't seem to be an enum-specific issue but a problem about multiple imports of the same module at different places. I'll try to clean up in the overall structure to get consistent imports.

karsten281
  • 43
  • 6
  • Can you provide the definition of the `InstrumentTypes` enum? – user2246849 May 05 '22 at 13:28
  • Do you do anything like reloading the module containing the enum or redefine the enum class? – Iain Shelvington May 05 '22 at 13:33
  • @user2246849 The InstrumentTypes enum is equally simple: `class InstrumentTypes(Enum): UNDEFINED = auto() SPRT = auto() THERMOCOUPLE = auto() FURNACE = auto() FIXEDPOINTCELL = auto()` – karsten281 May 05 '22 at 13:38
  • Make sure your module imports are consistent, Python could see them as different modules. E.g., if the enum module is not the top module (let's say you have Top.InstrumentType), you should always use them as Top.InstrumentType. Otherwise, Python will see them as different modules. – user2246849 May 05 '22 at 13:41
  • @IainShelvington The module containing the enum is reloaded in a couple of places. – karsten281 May 05 '22 at 13:43
  • @user2246849 Probably you are right about the import. It's far from consistent and straightforward as I'm new to Python. How can I avoid to reload the same module a couple of times? – karsten281 May 05 '22 at 13:45
  • @karsten281 when the module is reloaded the enum in the module is now a completely different class, any instance created before the reload will not be equal to any created after – Iain Shelvington May 05 '22 at 13:45
  • Maybe it's best if you edit the post with the file structure with the various Python scripts and show where the imports happen (and also how you import) – user2246849 May 05 '22 at 13:48
  • Thank you so far. I'll look into the import order of the different modules and which modules need to be imported at which place. The enum is defined in a "definition" module (translation of specific phrases) which is needed and therefore imported virtually everywhere else. So, I'm pretty sure that's the problem and it's not an enum specific issue. – karsten281 May 05 '22 at 13:58

1 Answers1

3

As indicated in the comments to my question it was an issue about multiple imports of the same module, basically the same as in this answer. After removing a subdirectory of my main class directory from PYTHONPATH and correcting the import statements throughout my code, the enums are compared correctly to each other. Thank you for your contributions.

karsten281
  • 43
  • 6