关于python:int()和long()之间的区别

Difference between int() and long()

python中int(x)和long(x)有什么区别

我的理解:

  • long()将始终返回long
  • int()将返回一个int或long(如果太大)
  • 所以int()足以根据其值动态获取int / long
  • 因此,除非以上(1)(2)(3)不正确,否则为什么需要long()? 当int()完成工作时?
    跳过所有数字范围的long()会伤害我吗?

    参考文档:

    类int(x = 0)

    Return an integer object constructed from a number or string x, or
    return 0 if no arguments are given. If x is a number, it can be a
    plain integer, a long integer, or a floating point number. If x is
    floating point, the conversion truncates towards zero. If the argument
    is outside the integer range, the function returns a long object
    instead.

    类长(x = 0)

    Return a long integer object constructed from a string or number x. If
    the argument is a string, it must contain a possibly signed number of
    arbitrary size, possibly embedded in whitespace. The base argument is
    interpreted in the same way as for int(), and may only be given when x
    is a string. Otherwise, the argument may be a plain or long integer or
    a floating point number, and a long integer with the same value is
    returned. Conversion of floating point numbers to integers truncates
    (towards zero). If no arguments are given, returns 0L.

    实验代码

    1
    2
    3
    4
    5
    number = int(number_string) # cast it to integer
    print number,"\t", type(number)

    number = long(number_string) # cast it to long
    print number,"\t", type(number)


    int:整数; 等同于Python 2.x中的C long,Python 3.x中的非限定长度

    long:无限制长度的长整数; 仅在Python 2.x中存在

    因此,在python 3x和以上版本中,您可以使用int(),而无需使用long()。

    希望这能消除您的疑问?