L suffix in long integer in Python 3.x

2024/10/12 22:26:45

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?

Answer

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.

https://en.xdnf.cn/q/69602.html

Related Q&A

Custom Colormap

I want to plot a heatmap with a custom colormap similar to this one, although not exactly.Id like to have a colormap that goes like this. In the interval [-0.6, 0.6] the color is light grey. Above 0.6,…

Whats the point of @staticmethod in Python?

Ive developed this short test/example code, in order to understand better how static methods work in Python.class TestClass:def __init__(self, size):self.size = sizedef instance(self):print("regul…

logical or on list of pandas masks

I have a list of boolean masks obtained by applying different search criteria to a dataframe. Here is an example list containing 4 masks: mask_list = [mask1, mask2, mask3, mask4]I would like to find th…

How to view the implementation of pythons built-in functions in pycharm?

When I try to view the built-in function all() in PyCharm, I could just see "pass" in the function body. How to view the actual implementation so that I could know what exactly the built-in f…

How to gracefully fallback to `NaN` value while reading integers from a CSV with Pandas?

While using read_csv with Pandas, if i want a given column to be converted to a type, a malformed value will interrupt the whole operation, without an indication about the offending value.For example, …

Python - object layout

can somebody describe the following exception? What is the "object layout" and how it is defined? ThanksTraceback (most recent call last):File "test_gui.py", line 5, in <module…

Using Tor proxy with scrapy

I need help setting up Tor in Ubuntu and to use it within scrapy framework.I did some research and found out this guide:class RetryChangeProxyMiddleware(RetryMiddleware):def _retry(self, request, reaso…

Best practice for structuring module exceptions in Python3

Suppose I have a project with a folder structure like so./project__init__.pymain.py/__helpers__init__.pyhelpers.py...The module helpers.py defines some exception and contains some method that raises th…

How can you read a gzipped parquet file in Python

I need to open a gzipped file, that has a parquet file inside with some data. I am having so much trouble trying to print/read what is inside the file. I tried the following: with gzip.open("myFil…

Pandas - combine row dates with column times

I have a dataframe:Date 0:15 0:30 0:45 ... 23:15 23:30 23:45 24:00 2004-05-01 3.74618 3.58507 3.30998 ... 2.97236 2.92008 2.80101 2.6067 2004-05-02 3.09098 3.846…