Can I change Enum type from string to bytes?

eucristian :

I am trying to send over sockets an Enum value, but however no matter what I try it gets encoded as a string (except when I manually convert it to bytes in the call to the socket.send)

class Example(Enum):
    A = b'example'
    B = bytes('example', 'utf8')

I call the send method from the socket module where sock is a prior bound socket for a server

....
conn, addr = sock.accept() 
conn.send(Example.A.name)

and the exception message is:

a bytes-like object is required, not 'str'

I have read this and some other links but I could not find the answer.

C.Nivs :

Enum is built on OrderedDict, so name corresponds to key and value is, well, value. Change to:

Example.A.value

Which has a bytes type, unlike the name which has a string type (A)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=168913&siteId=1