英文文档:

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 using str.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的更多相关文章

  1. Python内置函数(7)——bytearray

    英文文档: class bytearray([source[, encoding[, errors]]]) Return a new array of bytes. The bytearray cla ...

  2. Python内置函数(13)——complex

    英文文档: class complex([real[, imag]]) Return a complex number with the value real + imag*1j or convert ...

  3. 【转】python 内置函数总结(大部分)

    [转]python 内置函数总结(大部分) python 内置函数大讲堂 python全栈开发,内置函数 1. 内置函数 python的内置函数截止到python版本3.6.2,现在python一共为 ...

  4. python内置函数,匿名函数

    一.匿名函数 匿名函数:为了解决那些功能很简单的需求而设计的一句话函数 def calc(n): return n**n print(calc(10)) #换成匿名函数 calc = lambda n ...

  5. python 内置函数总结(大部分)

    python 内置函数大讲堂 python全栈开发,内置函数 1. 内置函数 python的内置函数截止到python版本3.6.2,现在python一共为我们提供了68个内置函数.它们就是pytho ...

  6. 【286】◀▶ Python 内置函数说明

    参考: Python 内置函数 01   abs() 返回数字的绝对值. 02   all() 用于判断给定的可迭代参数 iterable 中的所有元素是否不为 0.''.False 或者 itera ...

  7. Python内置函数和内置常量

    Python内置函数 1.abs(x) 返回一个数的绝对值.实参可以是整数或浮点数.如果实参是一个复数,返回它的模. 2.all(iterable) 如果 iterable 的所有元素为真(或迭代器为 ...

  8. Python | 内置函数(BIF)

    Python内置函数 | V3.9.1 | 共计155个 还没学完, 还没记录完, 不知道自己能不能坚持记录下去 1.ArithmeticError 2.AssertionError 3.Attrib ...

  9. python内置函数

    python内置函数 官方文档:点击 在这里我只列举一些常见的内置函数用法 1.abs()[求数字的绝对值] >>> abs(-13) 13 2.all() 判断所有集合元素都为真的 ...

随机推荐

  1. Android 运行时权限及APP适配

    Android 6.0起,Android加强了权限管理,引入运行时权限概念.对于: 1. Android 5.1(API 22)及以前版本,应用权限必须声明在AndroidManifest.xml中, ...

  2. 杭电oj-1000-A+B

    Problem Description Calculate A + B. Input Each line will contain two integers A and B. Process to e ...

  3. 实用的Docker入门

    1 Docker概述 Docker和虚拟机一样,都拥有环境隔离的能力,但它比虚拟机更加轻量级,可以使资源更大化地得到应用.首先来看Docker的架构图: 理解其中几个概念: Client(Docker ...

  4. spring之事务

    1.编程式事务 2.声明式事务:aop

  5. python中的return的返回与执行

    author:headsen  chen date:2018-03-21  15:12:09 notice:created by  headsen chen himself  and not allo ...

  6. 使用python读取word,写入execl

    word里面有2张表,需要找到第二张表,并写入execl中: 代码如下: #coding:utf-8 import os from docx import Document import win32c ...

  7. 通过Beego将之前实现的短url项目实现

    正好通过这个小例子对之前了解的beego框架的基本内容进行一个简单的应用 实现的完整代码地址:https://github.com/pythonsite/go_simple_code/tree/mas ...

  8. SIMD---SSE系列及效率对比

    SSE(即Streaming SIMD Extension),是对由MMX指令集引进的SIMD模型的扩展.我们知道MMX有两个明显的缺点: 只能操作整数. 不能与浮点数同时运行(MMX使用FPU寄存器 ...

  9. 1-1 spring基础

    1.spring是一个开源的轻量级的应用开发框架,它提供了IOC(Inversion of Control控制反转)和AOP(Aspect -Oriented Programming 面向切面编程)的 ...

  10. 笔记:Struts 2.3.31 配置说明

    复制文件到站点的 WEB-INF\lib 目录,文件列表如下,黄色突出显示的是必须加入的核心包 struts2-core-2.3.31.jar:struts2 的核心库 xwork-core-2.3. ...