Python Enums across Modules -


Why does not the parity in Python 3 enum be checked correctly in the boundaries of the module if the enum is in the main module Is defined? Here's an example:

Module Compression:

#! / Usr / bin / python3 Import Module B Reward Import.New, Unique @ Unique Class MyNEm (Enum): A = 1B = 2 # DEF __eq __ (self, others): # Artistance (Other, self .__ class__) # Return Self. Price == Other Value if __name__ == "__main__": myVar = MyEnum.B module. DoStuff (myVar)

module B.py:

  #! / Usr / bin / python3 import module is a def doStuff (aVariable): bVariable = moduleA.MyEnum "variant"  

on command line yield " ./ moduleA.py ": a

  traceback (most recent call final): file" ./moduleA.py ", line 17, & lt; Module & gt; Module B.Default (Mewar) file "/home/ruedi/Dropbox/Reps/stuffed/sosy/testing/moduleB.py", line 7, emphasizes a variable in the dstform == bVariable AssertionError  

Unmounting Custom Equality Operators in the Antum leads to a recorded failure. I have found that class module is not the same in both cases because it is "___ man__" in a case

edit: To correct this problem, What is the intangible way? Everywhere ", either" avial biwebble "does not work either:

  traceback (most recent call final): file" ./moduleA.py ", line 17, & lt; module & gt; moduleb .dstaf (Mavivar) file "/home/ruedi/Dropbox/Reps/stuffed/sosy/testing/moduleB.py", based on Route 7, a variable in dotstuff, the bariable attribute error  

As far as Python is concerned, you have the module three here:

  • __ Main __
  • Module A
  • < Li> module B

The file that runs from the command line, the main entry point, is always stored in the form of the __ main __ module If you import moduleA anywhere in the code, then Python sees that is different from the main __ module and instead creates a new module object. You type two different MyEnum sections:

  • __ main__. MyEnum
  • Module Myanm

Their members are different and can not be the same in this way.

Your test is passed if import moduleA you have used the import ___ man__ as module A, or different script file; That separate file will become the __ main___ :

  #! / Usr / bin / python3 # test.py, separate from the module, and modulebackage import module A. Import module B is another solution that Python will have to tell that  __ main __  and  < ___name__ == "__main__": myVar = moduleA.MyEnum.B moduleB.doStuff (myVar)  

code> module A is the same thing; first import module A (or module B , which import moduleA ) you enter another code in sys can add. Module :

  if __name__ == '__main__': import sys sys.modules ['moduleA'] = sys.modules ['__ main__'] Import module B  

I do not consider this very phenomenon.


Comments