Programming/Python
(Python) Enum to int
MeisterTJ
2023. 3. 27. 22:58
class EProtocol(Enum):
INFO = 0
TOKEN = 1
COMMAND = 2
CHAT = 3
위와 같은 Enum을
if __name__ == "__main__":
value = int(EProtocol.CHAT)
위와 같이 형변환을 하면
Traceback (most recent call last):
File "D:/Projects/Python/TCP_Test.py", line 28, in <module>
value = int(EProtocol.CHAT)
TypeError: int() argument must be a string, a bytes-like object or a number, not 'EProtocol'
위와 같은 에러가난다.
class EProtocol(Enum):
INFO = 0
TOKEN = 1
COMMAND = 2
CHAT = 3
def __int__(self):
return self.value
__int__ 를 구현해서 value 값인 숫자를 반환하게 하면 해결