osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Enums are Singletons - but not always?


Richard Damon <Richard at Damon-Family.org> writes:

> On 5/23/20 2:21 PM, Ralf M. wrote:
>> Hello,
>>
>> recently I wrote a small library that uses an Enum. That worked as
>> expected. Then I added a main() and if __name__ == "__main__" to make
>> it runable as script. Now Enum members that should be the same aren't
>> identical any more, there seem to be two instances of the same Enum.
>>
>> I think I know what's going on, but cannot find a good and elegant way
>> to avoid the problem. I hope someone here can help me there.
>
> I don;'t think Python anywhere defines that a enum will be a singleton,
> and you should be checking for equality (==) not identity (is)
>

https://docs.python.org/3/library/enum.html#enum-members-aka-instances

-- 
regards,
kushal