Python Enums across Modules
Warum kann in Python 3 die Enumeration über Modulgrenzen hinweg nicht korrekt überprüft werden, wenn die Enumeration im Hauptmodul definiert wurde? Hier ist ein Beispiel
moduleA.py:
#!/usr/bin/python3
import moduleB
from enum import Enum, unique
@unique
class MyEnum(Enum):
A = 1
B = 2
# def __eq__(self,other):
# assert isinstance(other,self.__class__)
# return self.value == other.value
if __name__ == "__main__":
myVar = MyEnum.B
moduleB.doStuff(myVar)
moduleB.py:
#!/usr/bin/python3
import moduleA
def doStuff(aVariable):
bVariable = moduleA.MyEnum.B
assert aVariable == bVariable
Calling ". / moduleA.py "in der Befehlszeile ergibt:
Traceback (most recent call last):
File "./moduleA.py", line 17, in <module>
moduleB.doStuff(myVar)
File "/home/ruedi/Dropbox/Reps/stuffed/sosy/testing/moduleB.py", line 7, in doStuff
assert aVariable == bVariable
AssertionError
Das Fehlen des benutzerdefinierten Gleichheitsoperators in der Aufzählung führt dort zu einem Assertionsfehler. Ich habe festgestellt, dass das Klassenmodul in beiden Fällen nicht dasselbe ist, da es in einem Fall "__main__" ist.
Was ist der "pythonischste Weg", um dieses Problem zu beheben (außer das Enum in ein eigenes Modul zu verschieben)?
BEARBEITEN Die Umstellung auf "aVariable ist bVariable" funktioniert auch nicht:
Traceback (most recent call last):
File "./moduleA.py", line 17, in <module>
moduleB.doStuff(myVar)
File "/home/ruedi/Dropbox/Reps/stuffed/sosy/testing/moduleB.py", line 7, in doStuff
assert aVariable is bVariable
AssertionError