[英]Behavior change of bytes from py2 to py3
I got curious after the discussion taking place on this question . 关于这个问题的讨论之后,我感到很好奇。 It appears that the behavior of
bytes()
has changed in python3. 看来在python3中
bytes()
的行为已更改。 In the docs for py3 it is now listed as a built-in function that behaves the same as bytearray()
except for the result being immutable. 在py3的文档中,它现在被列为内置函数,除了结果是不可变的以外,它的行为与
bytearray()
相同。 It doesn't appear in the same place in py2 docs. 在py2 docs中,它不会出现在同一位置。
In digging thru the docs for a while I couldn't really find anything detailing what's changed from 2 to 3, but it looks like something definitely has. 在研究文档一段时间时,我真的找不到任何详细说明从2变为3的变化,但是看起来确实有。 What's the difference and why was it changed?
有什么区别,为什么要更改?
From the linked question in the comments someone remarked with respect to py3 从评论中的链接问题中有人提到了py3
bytes(1) returns b'00'
bytes(1)返回b'00'
but in 2.7.5 但在2.7.5中
>>> bytes(1)
'1'
The Python 3 bytes
constructor takes an optional int
parameter specifying the number of bytes to output. Python 3
bytes
构造函数采用一个可选的int
参数,该参数指定要输出的字节数。 All bytes are initialized to 0 ( \\x00
) with that constructor, so bytes(1) == b'\\x00'
. 使用该构造函数将所有字节初始化为0(
\\x00
),因此bytes(1) == b'\\x00'
。
The Python 2 bytes
constructor is identical to str
, and therefore just stringizes its argument: Python 2
bytes
构造函数与str
相同,因此仅将其参数字符串化:
Python 2.7.5 (v2.7.5:ab05e7dd2788, May 13 2013, 13:18:45)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> bytes is str
True
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.