Python内置函数—bytearray
英文文档:
class bytearray
([source[, encoding[, errors]]])
Return a new array of bytes. The bytearray
class is a mutable sequence of integers in the range 0 <= x < 256. It has most of the usual methods of mutable sequences, described in Mutable Sequence Types, as well as most methods that the bytes
type has, see Bytes and Bytearray Operations.
The optional source parameter can be used to initialize the array in a few different ways:
- If it is a string, you must also give the encoding (and optionally, errors) parameters;
bytearray()
then converts the string to bytes usingstr.encode()
. - If it is an integer, the array will have that size and will be initialized with null bytes.
- If it is an object conforming to the buffer interface, a read-only buffer of the object will be used to initialize the bytes array.
- If it is an iterable, it must be an iterable of integers in the range
0 <= x < 256
, which are used as the initial contents of the array
def __init__(self, source=None, encoding=None, errors='strict'): # known special case of bytearray.__init__
"""
bytearray(iterable_of_ints) -> bytearray
bytearray(string, encoding[, errors]) -> bytearray
bytearray(bytes_or_buffer) -> mutable copy of bytes_or_buffer
bytearray(int) -> bytes array of size given by the parameter initialized with null bytes
bytearray() -> empty bytes array Construct a mutable bytearray object from:
- an iterable yielding integers in range(256)
- a text string encoded using the specified encoding
- a bytes or a buffer object
- any object implementing the buffer API.
- an integer
# (copied from class doc)
"""
pass
bytearray
返回一个新的字节数组。
bytearray类是range 0 < = x < 256的一个可变序列。
它有大多数可变序列的常用方法,在可变序列类型中描述,以及大多数字节类型的方法,参见字节和Bytearray操作。
可选的源参数可以用几种不同的方式来初始化数组:
- 如果它是一个字符串,那么您还必须给出编码(以及可选的错误)参数;bytearray()然后使用str.encode()将字符串转换为字节。
- 如果它是一个整数,那么数组将具有这个大小,并将用null字节初始化。
- 如果它是符合缓冲区接口的对象,则将使用对象的只读缓冲区来初始化字节数组。
- 如果它是可迭代的,那么它必须是range 0 < = x < 256的整数的迭代,它被用作数组的初始内容
如果没有参数,则创建一个大小为0的数组。
说明:
1. 返回值为一个新的字节数组
2. 当3个参数都不传的时候,返回长度为0的字节数组
#!/usr/bin/python3 b = bytearray()
print(b)
print(len(b))
结果:
bytearray(b'')
0
3. 当source参数为字符串时,encoding参数也必须提供,函数将字符串使用str.encode方法转换成字节数组
b = bytearray('中文')
结果:
Traceback (most recent call last):
File "D:/py/day001/day1/test.py", line 3, in <module>
b = bytearray('中文')
TypeError: string argument without an encoding
encoding参数也必须提供,函数将字符串使用str.encode方法转换成字节数组
b = bytearray('中文', 'utf-8')
print(b)
print(len(b))
结果:
bytearray(b'\xe4\xb8\xad\xe6\x96\x87')
6
4. 当source参数为整数时,返回这个整数所指定长度的空字节数组
#!/usr/bin/python3 b = bytearray(5)
print(b)
print(len(b))
执行结果:
bytearray(b'\x00\x00\x00\x00\x00')
5
5. 当source参数为实现了buffer接口的object对象时,那么将使用只读方式将字节读取到字节数组后返回
#!/usr/bin/python3 b = bytearray([1,2,3,4,5])
print(b)
print(len(b))
执行结果:
bytearray(b'\x01\x02\x03\x04\x05')
5
6. 当source参数是一个可迭代对象,那么这个迭代对象的元素都必须符合0 <= x < 256,以便可以初始化到数组里
#!/usr/bin/python3 b = bytearray([1,2,3,4,5,256])
print(b)
print(len(b))
执行结果:
Traceback (most recent call last):
File "D:/py/day001/day1/test.py", line 3, in <module>
b = bytearray([1,2,3,4,5,256])
ValueError: byte must be in range(0, 256)
Python内置函数—bytearray的更多相关文章
- python 内置函数bytearray
1.参考文档 class bytearray([source[, encoding[, errors]]]) Return a new array of bytes. The bytearray cl ...
- Python内置函数(12)——str
英文文档: class str(object='') class str(object=b'', encoding='utf-8', errors='strict') Return a string ...
- Python内置函数(61)——str
英文文档: class str(object='') class str(object=b'', encoding='utf-8', errors='strict') Return a string ...
- Python 内置函数笔记
其中有几个方法没怎么用过, 所以没整理到 Python内置函数 abs(a) 返回a的绝对值.该参数可以是整数或浮点数.如果参数是一个复数,则返回其大小 all(a) 如果元组.列表里面的所有元素都非 ...
- 【转】python 内置函数总结(大部分)
[转]python 内置函数总结(大部分) python 内置函数大讲堂 python全栈开发,内置函数 1. 内置函数 python的内置函数截止到python版本3.6.2,现在python一共为 ...
- python内置函数,匿名函数
一.匿名函数 匿名函数:为了解决那些功能很简单的需求而设计的一句话函数 def calc(n): return n**n print(calc(10)) #换成匿名函数 calc = lambda n ...
- python 内置函数总结(大部分)
python 内置函数大讲堂 python全栈开发,内置函数 1. 内置函数 python的内置函数截止到python版本3.6.2,现在python一共为我们提供了68个内置函数.它们就是pytho ...
- Python之路(第八篇)Python内置函数、zip()、max()、min()
一.python内置函数 abs() 求绝对值 例子 print(abs(-2)) all() 把序列中每一个元素做布尔运算,如果全部都是true,就返回true, 但是如果是空字符串.空列表也返回t ...
- python内置函数大全(分类)
python内置函数大全 python内建函数 最近一直在看python的document,打算在基础方面重点看一下python的keyword.Build-in Function.Build-in ...
随机推荐
- 贪心 Codeforces Round #304 (Div. 2) B. Soldier and Badges
题目传送门 /* 题意:问最少增加多少值使变成递增序列 贪心:排序后,每一个值改为前一个值+1,有可能a[i-1] = a[i] + 1,所以要 >= */ #include <cstdi ...
- git导出代码
1.快速查询 $git archive --format zip --output "./output.zip" master -0 ./output.zip 是生成的文件 mas ...
- pyinstaller打包遇到的错误处理
在python环境中运行中没问题,但打包时遇到以下问题 1.有时打包出现 UnicodeDecodeError错误, 可以改变cmd的编码(暂且这么叫),在cmd 中输入 chcp 65001,再次打 ...
- C#结构体和类的区别(转)
结构体和类的区别: 在做一个项目时,使用了较多的结构体,并且存在一些结构体的嵌套,即某结构体成员集合包含另一个结构体等,总是出现一些奇怪的错误,才终于下决心好好分析一下到底类和结构体有啥不同,虽 ...
- phpcms v9模板制作教程
phpcms v9模板制作教程(转载) 第一节 1.首先下载phpcms v9的集成安装包并安装,这里就不详细说明了. 2.本地调试建议大家使用APMserver,或者wampserver等,可以到P ...
- 原创 Repeater radio 单选和多选混合
希望高手朋友给我留下美好的意见,在此先感谢您! 前台代码repeater: <script src="../Scripts/jquery-1.9.1.js"></ ...
- Android Studio V4 V7 包冲突的问题
最近被包冲突的问题搞奔溃了,特别是V4,V7 V4和V7包冲突的解决方式就是!版本要一致!! 比如我的一个项目中应用本来是这样引用包的 compile 'com.android.support:sup ...
- (转)Synopsys工具简介
DC Ultra--Design Compiler的最高版本 在Synopsys软件中完整的综合方案的核心是DC UltraTM,对所有设计而言它也是最好级别的综合平台.DC Ultra添加了全面的数 ...
- 关于bin和obj文件夹。debug 和release的区别(转)
关于bin和obj文件夹. 楼主hcaihao(影子男孩)2002-05-29 20:04:24 在 .NET技术 / C# 提问 VS.Net会生成bin和obj文件夹以及它们下面的Debug和Re ...
- 如何理解Python中的if __name__ == '__main__'
1. 摘要 通俗的理解__name__ == '__main__':假如你叫小明.py,在朋友眼中,你是小明(__name__ == '小明'):在你自己眼中,你是你自己(__name__ == '_ ...