简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM