Comparing Enum values ​​in IronPython

In an IronPython script, I'm trying to compare a property (type Grade

) of an object (created in C #) with one of the possible enum values Grade

. For example:

if (myObject.TotalGrade == Grade.Fail):

      

Now I have set a breakpoint after getting both of these values, and I can actually check that both contain "Fail"; however, the check is not performed and the program proceeds to the next item. Is the python equality operator unusable for C # enums?

More info: I imported an enum Grade

from my C # as shown below.

import clr
clr.AddReferenceToFile("mydll.dll")
from mydll import Grade

      

+3


source to share


1 answer


As requested, my comment as an answer:

I'm not sure why they wouldn't compare equal, but apparently enums are implemented as nested types in IronPython - it seems you have two different objects for the same enum value in this case.



You can work around this by comparing the base values ​​directly like so:

if myObject.TotalGrade.value__ == Grade.Fail.value__:
    pass  # your code here...

      

+3


source







All Articles