[python] 从python2.x迁移到python3.x的常见操作

  1. print 'something' > print('something')
  2. xrange() > range()
  3. 除法division
    python 2.x : 整数 / 整数
    python 3.x : 整数 // 整数
    这个很难发现,因为python弱类型。
    python2.x中/是classic division,和C++一样,所以两个整数操作数会有整数结果,特别是可以自动取整。在python3.x中/是浮点除法,结果是浮点数,//是整数除法。

    python 2.x : integer / integer
    python 3.x : integer // integer
    this bug is hard to find as we cannot check the types since python does not require explicit declaration.
    / in python 2.x is the class division the same as in C++. Hence two integers as operands result in an integer. If the inherent flooring feature is desired, awareness should be raised when transferring to python 3.x. In python 3, / is used as merely float division and will produce a float in the above case, so // should be used to replace it because it supports the integer division.

你可能感兴趣的:(坑)