I have MyEnum
, an enumerate derived from enum
, defined in a myenum.py
file such as :
# myenum.py
import enum
class MyEnum(enum.Enum):
ONE = 1
TWO = 2
Then, I import this file using the importlib.import_module()
method. I create a
an instance of my enumerate, and test its value : it is correct, as intended.
However, if I reload my file, using importlib.reload()
, a
is no longer equal to MyEnum.ONE
. What is causing this ? I'm on Python 3.7.
# test.py
import importlib
def test_enum(e):
print(e, myenum.MyEnum.ONE)
print("test is :", e==myenum.MyEnum.ONE)
if __name__=='__main__':
globals()["myenum"] = importlib.import_module("myenum")
a = myenum.MyEnum.ONE
test_enum(a)
importlib.reload(globals()["myenum"])
test_enum(a)
Result
MyEnum.ONE MyEnum.ONE
test is : True
MyEnum.ONE MyEnum.ONE
test is : False
Edit : After further research, it appears enums in Python are compared by IDs. However, when reimporting the module, the IDs of my enum are changed, which is why the comparison returns False
.
What options would there be to avoid this ID change or allow the comparison to stay True ?