Reputation: 2154
I have the code like:
max32 = 0xffffffffL
print(sys.getsizeof(max32))
>>32
which is written in python 2.
Now I need make sure this code is also compatible in python 3. But there is no long integer in python3. How do I do that?
Upvotes: 0
Views: 5226
Reputation: 14360
The int
in python3 is mostly compatible with int
in python2. Code that works with python2 will work in python3 in this regard except code that rely on:
int
s resulting in the same type (if the result doesn't fit into an int the result will be long in python2)long
type (you can still define a function long
that does the same thing in python3)L
suffix...and probably some more situations which I may have overlooked.
The rationale is that with only one integral type you have an integral type that acts just like an integer is supposed to behave - with one such type there is more or less no need for other integral types that does not behave as integers are supposed to behave (or require the long type to supplement them). The exception is of course situations where you want "mod 2^n" behaviour (like java integral types, where 2*0x7FFFFFFF may become -2), which python2 int
s didn't support anyway, maybe there's a class for having that behavior.
Upvotes: 0
Reputation: 90889
Using PEP 0237 - long
has been renamed to int
, just remove the L
and use it. Example -
>>> max64 = 0xffffffffffffffff
>>> max64
18446744073709551615
Also from Whats new in Python 3.0 -
PEP 0237: Essentially, long renamed to int. That is, there is only one built-in integral type, named int; but it behaves mostly like the old long type.
Also, in Python 3.x , seems like int has variable size depending on the integer stored in it -
>>> max64 = 0xffffffffffffffff
>>> sys.getsizeof(max64)
22
>>> max32 = 0xffffffff
>>> sys.getsizeof(max32)
18
>>> max67 = 0xffffffffffffffffffffffffffffff
>>> sys.getsizeof(max67)
28
>>> max100 = 100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
>>> sys.getsizeof(max100)
68
So you should not depend on the size of bytes in your code.
Upvotes: 6