On 31Aug2018 21:44, Marko Rauhamaa <ma...@pacujo.net> wrote:
Malcolm Greene <pyt...@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 <c...@cskk.id.au>
--
https://mail.python.org/mailman/listinfo/python-list