英文文档及翻译

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

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类是range 0 < = x < 256的一个可变序列。

它有大多数可变序列的常用方法,在可变序列类型中描述,以及大多数字节类型的方法,参见字节和Bytearray操作。

可选的源参数可以用几种不同的方式来初始化数组:

如果它是一个字符串,那么您还必须给出编码(以及可选的错误)参数;bytearray()然后使用str.encode()将字符串转换为字节。

如果它是一个整数,那么数组将具有这个大小,并将用null字节初始化。

如果它是符合缓冲区接口的对象,则将使用对象的只读缓冲区来初始化字节数组。

如果它是可迭代的,那么它必须是range 0 < = x < 256的整数的迭代,它被用作数组的初始内容

如果没有参数,则创建一个大小为0的数组。

说明

返回值为一个新的字节数组

当3个参数都不传的时候,返回长度为0的字节数组

#!/usr/bin/python3

b = bytearray()

print(b)
print(len(b))

结果:

bytearray(b'')

0

当source参数为字符串时,encoding参数也必须提供,函数将字符串使用str.encode方法转换成字节数组

b = bytearray('中文')

结果:

Traceback (most recent call last):

File "D:/py/day001/day1/test.py", line 3, in

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

当source参数为整数时,返回这个整数所指定长度的空字节数组

#!/usr/bin/python3

b = bytearray(5)

print(b)

print(len(b))

执行结果:

bytearray(b'\x00\x00\x00\x00\x00')

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

当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

b = bytearray([1,2,3,4,5,256])

ValueError: byte must be in range(0, 256)

python中bytearray函数_Python内置函数—bytearray相关推荐

  1. python中dir用法_Python内置函数dir详解

    1.命令介绍 最近学习并使用了一个python的内置函数dir,首先help一下: >>> help(dir) Help on built-in function dir in mo ...

  2. python中的作用域以及内置函数globals()-全局变量、locals()-局部变量

    在python中,函数会创建一个自己的作用域,也称为为命名空间.这意味着在函数内部访问某个变量时,函数会优先在自己的命名空间中寻找. 通过内置函数globals()返回的是python解释器能知道的变 ...

  3. Python中这两个内置函数locals 和globals,你了解吗?

    这两个函数主要提供,基于字典的访问局部和全局变量的方式. 在理解这两个函数时,首先来理解一下python中的名字空间概念.Python使用叫做名字空间的 东西来记录变量的轨迹.名字空间只是一个字典,它 ...

  4. decode函数_Python 内置函数总一

    内置函数 python内置函数,截至python3.6.2版本之前一共68个内置函数,内置函数是可以直接使用的函数. Python标准库/内置函数链接:点击此处 内置函数分类 作用域相关 基于字典的形 ...

  5. python内置函数返回序列中最大元素_Python 内置函数 ____________ 用来返回序列中的最大元素。_学小易找答案...

    [单选题]5. an official group of people who have joined together for a particular purpose [单选题]Excel 201 ...

  6. python内置方法就是内置函数_python内置函数

    什么是内置函数? 就是python给你提供的,拿来直接用的函数, 比如print 和 input等等. 截止到python版本3.6.2 python一共提供了68个内置函数. 他们就是python直 ...

  7. python filter内置函数_python 内置函数filter

    python 内置函数filter class filter(object): """ filter(function or None, iterable) --> ...

  8. python 中一些常用的内置函数

    一.常用内置函数 abs(x) 返回绝对值,参数为int float,非字符只能num all(iterable) 如果迭代对象里面的所有值都为真就返回True.all([1, 2, -7]) --- ...

  9. python内置的数字运算函数_Python 内置函数1

    abs(x)函数 返回绝对值 参数可以是:负数.正数.浮点数或者长整形 print(abs(-1.2)) # 结果1.2 cmp(x, y)函数 (python3已删) 中文说明: 比较两个对象x和y ...

  10. python property函数_Python内置函数property()如何使用

    代码 class Shuxing(): def __init__(self, size = 10): self.size = size def getSize(self): print('getSiz ...

最新文章

  1. Splunk学习与实践
  2. Python入门100题 | 第064题
  3. jQuery最佳实践
  4. 利用URL拼接爬取获取有道翻译内容
  5. php试题及答案 博客,转php面试题及我的答案(一)
  6. 【Latex】修改文章字号的几种方式
  7. Valak 6个月上演“变身戏法”,紧盯 Exchange 服务器窃取企业数据
  8. 嵌入式系统开发之中断控制的实现
  9. 为什么现在我最终推荐内存OLTP
  10. 搜索引擎-应用篇(地理位置查询)
  11. 如何用TortoiseSVN将项目代码提交到SVN
  12. Eclipse安装Kotlin插件后,新建找不到Kotlin文件
  13. 魔兽世界活跃人数持续下降
  14. Android手机扫描,电脑复制内容----手机实现无线扫码枪功能
  15. 利用vscode高效阅读《You Don't Know JS》
  16. 戴尔科技:“强平台+即服务”创新模式,重新定义混合多云之路
  17. API Store使用步骤
  18. [原创] 海外地图服务Here Map在Android 端的使用介绍
  19. 浅谈科学与艺术交融——艺工交叉
  20. 子网掩码划分计算方法及实例

热门文章

  1. 成都盛铭轩电商:活动图片如何设计
  2. SpringSecurity认证案例
  3. git 码云 简要使用
  4. 为什么我加了过滤器然后就登不进去了_化隆空气过滤器滤芯哪里有
  5. java中将Object类型转换成String类型
  6. python写入文件没反应_记第一个问题——python文件无法写入数据
  7. nacos注册成功但是服务管理界面没有内容
  8. 《大话处理器》简要学习笔记
  9. 华为云服务器最新信息,云服务器拉新
  10. 微信小程序开发消息推送配置教程