0

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?

city
  • 2,036
  • 2
  • 32
  • 40
  • Might want to go through the answers present here: http://stackoverflow.com/questions/2104884/how-does-python-manage-int-and-long and also look at: https://www.python.org/dev/peps/pep-0237/ – praxmon Jul 21 '15 at 05:26
  • @PrakharMohanSrivastava, I edited my question. What I want is to make sure the long number in python 3 also has same size as in python 2. – city Jul 21 '15 at 05:29
  • "What I want is to make sure the long number in python 3 also has same size as in python 2" - why are you relying on *that*? – user2357112 Jul 21 '15 at 05:29
  • You simply can't. On my python 2 you get the answer `36` on above snippet - if you can't assume that the size is the same on different implementations of python 2, why do you think that you can rely on it to be same on python 3? – skyking Jul 21 '15 at 05:31
  • @skyking, it turns out I asked a silly question.. – city Jul 21 '15 at 05:38
  • Why do you need that? In Python 3 , int uses variable number of bytes depending on the size. – Anand S Kumar Jul 21 '15 at 05:38

2 Answers2

6

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.

Anand S Kumar
  • 88,551
  • 18
  • 188
  • 176
0

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:

  • sys.getsizeof(x) returning the same result (which it doesn't anyway)
  • arithmetic expression containing ints resulting in the same type (if the result doesn't fit into an int the result will be long in python2)
  • code that relies on the existence of a long type (you can still define a function long that does the same thing in python3)
  • code that relies on the 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 ints didn't support anyway, maybe there's a class for having that behavior.

skyking
  • 13,817
  • 1
  • 35
  • 57