在Python 2.x中,L在长整数后面有一个后缀.由于Python 3将所有整数视为长整数,因此已将其删除.从Python 3.0中的新功能:
长整数的repr()不再包含尾随L,因此无条件地剥离该字符的代码将切掉最后一位数字.(改用str().)
从这里我得到的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'
Run Code Online (Sandbox Code Playgroud)
应该不是的输出str()是18446744073709551616L按该文档?我找不到任何东西有什么新的Python 3.1,有什么新的Python 3.2和有什么新的Python 3.3,上面写着L后缀从删除str()了.3.2说:
浮点数或复数的str()现在与其repr()相同.
但它没有说整数.
从哪个版本的Python L后缀中删除str()?还是我错过了一些明显的东西?
Mar*_*ers 12
你误解了文档.
这句话的目的是试图在Python 2中剥离L出来的人.那些人可以使用而不是每次都去掉相同的数字.repr() str()L
换句话说,str()当在Python 2中的长整数上使用时,将数字转换为字符串是更好的方法,因为它永远不会添加将添加的L后缀repr():
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
Run Code Online (Sandbox Code Playgroud)
Python 3永远不会添加L.不是在使用时repr(),而是在使用时str().没有意义; Python 3中的所有整数都是长整数.
| 归档时间: |
|
| 查看次数: |
9029 次 |
| 最近记录: |