Python内置函数(13)——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.
Without an argument, an array of size 0 is created.
根据传入的参数创建一个新的字节数组
说明:
1. 返回值为一个新的字节数组
2. 当3个参数都不传的时候,返回长度为0的字节数组
>>> b = bytearray()
>>> b
bytearray(b'')
>>> len(b)
0
3. 当source参数为字符串时,encoding参数也必须提供,函数将字符串使用str.encode方法转换成字节数组
>>> bytearray('中文')
Traceback (most recent call last):
File "<pyshell#48>", line 1, in <module>
bytearray('中文')
TypeError: string argument without an encoding
>>> bytearray('中文','utf-8')
bytearray(b'\xe4\xb8\xad\xe6\x96\x87')
4. 当source参数为整数时,返回这个整数所指定长度的空字节数组
>>> bytearray(2)
bytearray(b'\x00\x00')
>>> bytearray(-2) #整数需大于0,使用来做数组长度的
Traceback (most recent call last):
File "<pyshell#51>", line 1, in <module>
bytearray(-2)
ValueError: negative count
5. 当source参数为实现了buffer接口的object对象时,那么将使用只读方式将字节读取到字节数组后返回
6. 当source参数是一个可迭代对象,那么这个迭代对象的元素都必须符合0 <= x < 256,以便可以初始化到数组里
>>> bytearray([1,2,3])
bytearray(b'\x01\x02\x03')
>>> bytearray([256,2,3]) #不在0-255范围内报错
Traceback (most recent call last):
File "<pyshell#53>", line 1, in <module>
bytearray([256,2,3])
ValueError: byte must be in range(0, 256)
Python内置函数(13)——bytearray的更多相关文章
- Python内置函数(7)——bytearray
英文文档: class bytearray([source[, encoding[, errors]]]) Return a new array of bytes. The bytearray cla ...
- Python内置函数(13)——complex
英文文档: class complex([real[, imag]]) Return a complex number with the value real + imag*1j or convert ...
- 【转】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 ...
- 【286】◀▶ Python 内置函数说明
参考: Python 内置函数 01 abs() 返回数字的绝对值. 02 all() 用于判断给定的可迭代参数 iterable 中的所有元素是否不为 0.''.False 或者 itera ...
- Python内置函数和内置常量
Python内置函数 1.abs(x) 返回一个数的绝对值.实参可以是整数或浮点数.如果实参是一个复数,返回它的模. 2.all(iterable) 如果 iterable 的所有元素为真(或迭代器为 ...
- Python | 内置函数(BIF)
Python内置函数 | V3.9.1 | 共计155个 还没学完, 还没记录完, 不知道自己能不能坚持记录下去 1.ArithmeticError 2.AssertionError 3.Attrib ...
- python内置函数
python内置函数 官方文档:点击 在这里我只列举一些常见的内置函数用法 1.abs()[求数字的绝对值] >>> abs(-13) 13 2.all() 判断所有集合元素都为真的 ...
随机推荐
- 用jquery实现日期控件
用jquery实现的日期控件,代码如下: <!DOCTYPE html> <html lang="en"> <head> <meta ch ...
- 记kkpager分页控件的使用
kkpager支持异步加载分页: 1.页面添加div标签和引用JS,默认标签为<div id="kkpager"></div> 引用JS和样式 <sc ...
- python爬微信公众号前10篇历史文章(2)-拼接URL&发送http请求
如何拼接想要的url http://weixin.sogou.com/weixin?type=1&page=1&ie=utf8&query=%E5%A4%A7%E7%BA%BD ...
- linux下安装软件
业界的软件标准安装有三步:configure,make,make install,下面是它们的定义: ./configure是用来检测你的安装平台的目标特征的.比如它会检测你是不是有CC或GCC,并不 ...
- 用IDEA生成javadoc文档
用IDEA生成javadoc文档 打开相应的选项面板 设置 -encoding是java代码编码,-charset是对生成文档所用的编码.-windowtitle就是对应html的<title& ...
- CCF-CSP 201709-4通信网络
问题描述 某国的军队由N个部门组成,为了提高安全性,部门之间建立了M条通路,每条通路只能单向传递信息,即一条从部门a到部门b的通路只能由a向b传递信息.信息可以通过中转的方式进行传递,即如果a能将信息 ...
- GO语言初探
1.GO使用UTF-8编码,纯Unicode文本编写. 2.$ go verson (windows) 3.windows下,需要设置go语言的环境变量,新建一个名为 GOROOT的变量,指向go的具 ...
- js对象系列【一】深层理解对象与原型
我们先从盘古开天辟地时捋一捋对象: 从宏观内容来讲,javascript是一个属性的集合,包括值,函数,而整个集合也可以类比为一个对象. js = { a的变量名: a的值, ... 函数b: fun ...
- [poj2002]Squares_hash
Squares poj-2002 题目大意:在笛卡尔坐标系中给出n个点,求这些点可以构成多少个正方形. 注释:$1\le n\le 10^3$,$-2\cdot 10^3\le x , y\le 2\ ...
- DB2开发系列之四——触发器
1.触发器类型 1)BEFORE 触发器:在对表插入或更新之前执行该触发器,允许使用CALL 和 SIGNAL SQL 语句: 2)BEFORE DELETE 触发器:在删除操作之前执行该触发器: 3 ...