bytes() or .encode() for encoding str's as bytes?

Cameron Simpson cs at cskk.id.au
Fri Aug 31 17:05:43 EDT 2018


On 31Aug2018 21:44, Marko Rauhamaa <marko at pacujo.net> wrote:
>Malcolm Greene <python at bdurham.com>:
>> Is there a benefit to using one of these techniques over the other?
>> Is one approach more Pythonic and preferred over the other for
>> style reasons?
>> message = message.encode('UTF-8')
>> message = bytes(message, 'UTF-8')
>
>I always use the former. I wonder why that is. I guess the aesthetic
>rule is something along the lines: use a dot if you can.

I also use the former.

You could give any class an encode method; the latter works only for strings.  
Of course, an encode method accepting a Unicode encoding name is pretty string 
specific, but imagine:

  f.write(message.encode('UTF-8'))
  f.write(something.encode('big-endian'))

etc. A little more symmetric.

And I confess to not knowing of the "bytes(str, encoding)" form until now.

The flip side is that you can give classes a __bytes__ method, and go:

  f.write(bytes(something))

I've got a class which works that way, should the user want it.

Cheers,
Cameron Simpson <cs at cskk.id.au>



More information about the Python-list mailing list