加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

Python 3.x中的长整数的L后缀

发布时间:2020-12-20 12:06:30 所属栏目:Python 来源:网络整理
导读:在 Python 2.x中,在长整数后面有一个L后缀.由于Python 3将所有整数视为长整数,因此已将其删除.从 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 charac
在 Python 2.x中,在长整数后面有一个L后缀.由于Python 3将所有整数视为长整数,因此已将其删除.从 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.)

从这里我得到repr()不会显示L后缀,但str()将具有L后缀.但在Python 3.3.3中,没有一个显示L后缀.

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'

根据文档,str()的输出不应该是18446744073709551616L吗?我在What’s New In Python 3.1,What’s New In Python 3.2和What’s New In Python 3.3中找不到任何说明L后缀从str()中删除的内容. 3.2说:

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

但它没有说整数.

从str()中删除了哪个版本的Python L后缀?还是我错过了一些明显的东西?

解决方法

你误解了文档.

这句话的目的是试图从Python 2中的repr()中删除L.那些人可以使用str()来获得相同的数字,而不必每次都去掉L.

换句话说,str()在Python 2中的长整数上使用时,是将数字转换为字符串的更好方法,因为它永远不会添加repr()将添加的L后缀:

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","credits" or "license" for more information.
>>> print repr(1L)
1L
>>> print str(1L)
1

Python 3永远不会添加L.不使用repr()时,而不是使用str()时.没有意义; Python 3中的所有整数都是长整数.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读