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

Enums are Singletons - but not always?

Richard Damon schreef op 23/05/2020 om 20:57:
> 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)

So much this. Always check for equality instead of identity, unless you 
know what you're doing and you have a very good reason to use identity.

Everywhere I see questions all the time from people new to Python, 
confused about stuff that happens when comparing using identity. I would 
like to know where that comes from ... are there tutorials that 
encourage using identity checks?

"Honest criticism is hard to take, particularly from a relative, a
friend, an acquaintance, or a stranger."
         -- Franklin P. Jones

Roel Schroeven