On 2020-10-22, Chris Angelico <ros...@gmail.com> wrote:
> On Fri, Oct 23, 2020 at 12:15 AM Shaozhong SHI <shishaozh...@gmail.com> wrote:

>> What should I know or watch out if I decide to move from Python 2.7
>> to Python 3?
>
> Key issues? Well, for starters, you don't have to worry about whether
> your strings are Unicode or not. They just are, unless you explicitly
> need them to be bytes.

The 'bytes' thing is important. If you use serial ports, sockets, or
anything else that's raw bytes, that code will need to be examined
carefully.

The usage of the 'bytes' type in 3.x isn't at all like 2.x:

    Python2:
    >>> bytes('abcd')
    'abcd'
    >>> bytes(3)
    '3'
    >>> bytes([0,1,2])
    '[0, 1, 2]'

    Python3:
    >>> bytes('abcd')
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: string argument without an encoding
    >>> bytes(3)
    b'\x00\x00\x00'
    >>> bytes([0,1,2])
    b'\x00\x01\x02
    
    Python2:
    >>> b'abcd'[2]
    'c'
    
    Python3:
    >>> b'abcd'[2]
    99

Moving from 2.x to 3.x isn't too bad, but trying to maintain
compatiblity with both is painful. At this point, I would probably
just abandon 2.x.

--
Grant



-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to