Reputation: 1173
I am use python3.8 and I create two services(ForwarderService
, LogService
) ,But it confused me why the two service type is the same and how solve it?
The Code
from enum import Enum, auto
import abc
class ServiceType(Enum):
Forwarder = auto
Log = auto
class Service(metaclass=abc.ABCMeta):
@property
@abc.abstractmethod
def type(self) -> ServiceType:pass
class ForwarderService(Service):
@property
def type(self) -> ServiceType:
return ServiceType.Forwarder
class LogService(Service):
@property
def type(self) -> ServiceType:
return ServiceType.Log
s1 = ForwarderService()
s2 = LogService()
print(s1.type, s2.type)
The Result
ServiceType.Forwarder ServiceType.Forwarder
I want s1.type
is ServiceType.Forwarder
and s2.type
is ServiceType.Log
?
Upvotes: 1
Views: 35
Reputation: 96172
You have a typo:
class ServiceType(Enum):
Forwarder = auto
Log = auto
Should be:
class ServiceType(Enum):
Forwarder = auto()
Log = auto()
Otherwise, it interprets it as the same value, in which case, you end up with only one enum object for the enum type, with Log
acting as an alias.
So, consider:
>>> from enum import Enum
>>> class Foo(Enum):
... bar = 0
... baz = 0
...
>>> list(Foo)
[<Foo.bar: 0>]
>>> Foo.baz
<Foo.bar: 0>
Upvotes: 3