Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

L suffix in long integer in Python 3.x

In Python 2.x there was a L suffix after long integer. As Python 3 treats all integers as long integer this has been removed. From What's New In Python 3.0:

The repr() of a long integer doesn’t include the trailing L anymore, so code that unconditionally strips that character will chop off the last digit instead. (Use str() instead.)

From this I get that repr() won't show L suffix, but str() will have the L suffix. But in Python 3.3.3 none of them are showing L suffix.

Python 3.3.3 (v3.3.3:c3896275c0f6, Nov 18 2013, 21:19:30) [MSC v.1600 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> repr(2 ** 64)
'18446744073709551616'
>>> str(2 ** 64)
'18446744073709551616'

Shouldn't the output of str() be 18446744073709551616L as per the doc? I could not find anything in What's New In Python 3.1, What's New In Python 3.2 and What's New In Python 3.3 that says L suffix is removed from str() too. 3.2 says that:

The str() of a float or complex number is now the same as its repr().

But it says nothing about integer.

From which version of Python L suffix is removed in str() too? Or am I missing something obvious?

like image 233
taskinoor Avatar asked Jun 05 '14 12:06

taskinoor


People also ask

What is _L in Python?

Python supports arbitrary precision integers, meaning you're able to represent larger numbers than a normal 32 or 64 bit integer type. The L tells you when a literal is of this type and not a regular integer. Note, that L only shows up in the interpreter output, it's just signifying the type.

How do you declare a long int in Python 3?

Type int(x) to convert x to a plain integer. Type long(x) to convert x to a long integer.

Does Python 3 have long?

Python 3 does not have a long type.

Is there a long int in Python?

Python currently distinguishes between two kinds of integers (ints): regular or short ints, limited by the size of a C long (typically 32 or 64 bits), and long ints, which are limited only by available memory.


1 Answers

You misunderstood the documentation.

The remark is aimed at people trying to strip the L from repr() in Python 2. Those people could use str() instead and get the same number without having to strip the L each time.

In other words, str(), when used on a long integer in Python 2, is the better method to convert the number to a string, as it will never add the L suffix that repr() would add:

Python 2.7.6 (default, Apr 28 2014, 17:17:35) 
[GCC 4.2.1 Compatible Apple LLVM 5.1 (clang-503.0.40)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> print repr(1L)
1L
>>> print str(1L)
1

Python 3 will never add the L. Not when using repr(), and not when using str(). There would be no point; all integers in Python 3 are long integers.

like image 83
Martijn Pieters Avatar answered Sep 27 '22 21:09

Martijn Pieters