Enum with only a single member
Peter Otten
__peter__ at web.de
Tue Jan 10 04:52:26 EST 2017
Steven D'Aprano wrote:
> Is it silly to create an enumeration with only a single member? That is, a
> singleton enum?
>> from enum import Enum
>> class Unique(Enum):
> FOO = auto()
>>> The reason I ask is that I have two functions that take an enum argument.
> The first takes one of three enums:
>> class MarxBros(Enum):
> GROUCHO = 1
> CHICO = 2
> HARPO = 3
>> and the second takes *either* one of those three, or a fourth distinct
> value. So in my two functions, I have something like this:
>>> def spam(arg):
> if isinstance(arg, MarxBros):
> ...
>>> def ham(arg):
> if isinstance(arg, MarxBros) or arg is Unique.FOO:
> ...
>>> Good, bad or indifferent?
I don't see a problem. As it looks like you cannot subclass Enum subclasses
alternative spellings might be
isintance(args, (MarxBros, Unique))
or
BOTH = set(MarxBros) | set(Unique)
arg in MarxBros
arg in BOTH
More information about the Python-list
mailing list