Re: Enums are Singletons - but not always?

2020-05-23 Thread Terry Reedy
On 5/23/2020 2:21 PM, Ralf M. wrote: # Code of mod1.py # import enum, mod2 class En(enum.Enum):     A = 1     B = 2 def main():     a = mod2.getA()     print("a is En.A:", a is En.A)     print("a:", repr(a), "    En.A:", repr(En.A))     print("id(a), id(a.__class__)", id(a),

Re: Enums are Singletons - but not always?

2020-05-23 Thread Roel Schroeven
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

Re: Enums are Singletons - but not always?

2020-05-23 Thread Ethan Furman
On 05/23/2020 11:57 AM, Richard Damon wrote: I don't think Python anywhere defines that a enum will be a singleton, and you should be checking for equality (==) not identity (is) If you're not sure, please do a little research first. We have enough bad information on the 'nets already.

Re: Enums are Singletons - but not always?

2020-05-23 Thread Chris Angelico
On Sun, May 24, 2020 at 5:58 AM Kushal Kumaran wrote: > > "Ralf M." writes: > > > Below are a simplified code sample, the results when I run it and my > > thoughts. > > > > # Code of mod1.py # > > import enum, mod2 > > def main(): > > a = mod2.getA() > > # End of mod1.py # >

Re: Enums are Singletons - but not always?

2020-05-23 Thread Kushal Kumaran
"Ralf M." writes: > 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

Re: Enums are Singletons - but not always?

2020-05-23 Thread Peter Otten
Peter Otten wrote: >> # Code of mod2.py # > import __main__ as mod1 >> def getA(): >>return mod1.En.A >> # End of mod2.py # > > but that would hardcode the assumption that __main__ is always mod1.py. I should have mentioned the cyclic dependency -- if two modules import each

Re: Enums are Singletons - but not always?

2020-05-23 Thread Kushal Kumaran
Richard Damon 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 >>

Re: Enums are Singletons - but not always?

2020-05-23 Thread Peter Otten
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

Re: Enums are Singletons - but not always?

2020-05-23 Thread Richard Damon
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

Enums are Singletons - but not always?

2020-05-23 Thread Ralf M.
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