What is the difference between int(x) and long(x) in python
My understanding:
- long() will always return a long
- int() will return an int or a long (if its too big)
- so int() is sufficient to dynamically get a int/long based on its value
So unless above (1) (2) (3) are incorrect, why do you need long()? when int() gets the job done?
skipping long() for all number ranges will hurt me?
Documentation refered:
class 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.
class long(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.
code experimented
number = int(number_string) # cast it to integer
print number, "", type(number)
number = long(number_string) # cast it to long
print number, "", type(number)
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…