if bytes != str:

Ian Kelly ian.g.kelly at gmail.com
Sun Aug 4 11:06:26 EDT 2019


On Sun, Aug 4, 2019, 9:02 AM Hongyi Zhao <hongyi.zhao at gmail.com> wrote:

> Hi,
>
> I read and learn the the following code now:
>
> https://github.com/shadowsocksr-backup/shadowsocksr-libev/blob/master/src/
> ssrlink.py
> <https://github.com/shadowsocksr-backup/shadowsocksr-libev/blob/master/src/ssrlink.py>
>
> In this script, there are the following two customized functions:
>
>
> ----------
> def to_bytes(s):
>         if bytes != str:
>                 if type(s) == str:
>                         return s.encode('utf-8')
>         return s
>
> def to_str(s):
>         if bytes != str:
>                 if type(s) == bytes:
>                         return s.decode('utf-8')
>         return s
> ----------
>
> I've the following confusion on the above code:
>
> Why should use `if bytes != str:' here?  I mean, this will always return
> True, IMO.
>
> See my following test in ipython:
>
> In[20]: bytes != str
> Out[20]: True
>
>
> Any hints on this?
>

In Python 2.7, bytes and str are the same type.

>



More information about the Python-list mailing list