1.参考文档

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的数组。

说明:
1. 返回值为一个新的字节数组
2. 当3个参数都不传的时候,返回长度为0的字节数组

#!/usr/bin/python3
b = bytearray()
print(b)<br>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))

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

#!/usr/bin/python3
b = bytearray(5)
print(b)
print(len(b))

5. 当source参数为实现了buffer接口的object对象时,那么将使用只读方式将字节读取到字节数组后返回

#!/usr/bin/python3

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

print(b)

print(len(b))

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)

转载自 https://www.cnblogs.com/chenlin163/p/7261188.html

python 内置函数bytearray的更多相关文章

  1. Python内置函数—bytearray

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

  2. Python内置函数(12)——str

    英文文档: class str(object='') class str(object=b'', encoding='utf-8', errors='strict') Return a string  ...

  3. Python内置函数(61)——str

    英文文档: class str(object='') class str(object=b'', encoding='utf-8', errors='strict') Return a string ...

  4. Python 内置函数笔记

    其中有几个方法没怎么用过, 所以没整理到 Python内置函数 abs(a) 返回a的绝对值.该参数可以是整数或浮点数.如果参数是一个复数,则返回其大小 all(a) 如果元组.列表里面的所有元素都非 ...

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

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

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

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

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

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

  8. Python之路(第八篇)Python内置函数、zip()、max()、min()

    一.python内置函数 abs() 求绝对值 例子 print(abs(-2)) all() 把序列中每一个元素做布尔运算,如果全部都是true,就返回true, 但是如果是空字符串.空列表也返回t ...

  9. python内置函数大全(分类)

    python内置函数大全 python内建函数 最近一直在看python的document,打算在基础方面重点看一下python的keyword.Build-in Function.Build-in ...

随机推荐

  1. SVN服务端安装

    1 首先安装SVN和Subversion. 安装文件可自行百度. 2 在服务端创建版本库. 我的安装目录是c:\Program Files(x86)\Subversion. 安装完成后在安装目录下sh ...

  2. Android 简单计算器实现源码

    1.string.xml代码 <?xml version="1.0" encoding="utf-8"?> <resources> &l ...

  3. H3C系列之三层交换机dhcp功能的开启

    环境介绍>>>>>>>>>>>>>>>>>>>>交换机名牌:H3C交换机类型:三 ...

  4. AngularJS初始(一)

    什么是AngularJs? angularjs是一个为动态WEB应用设计的结构框架.它能让你使用HTML作为模板语言,通过扩展HTML的语法,让你能更清楚.简洁地构建你的应用组件.它的创新点在于,利用 ...

  5. windows下java开发资料汇总

    开发环境搭建:   (1) java开发环境配置    (2) maven环境快速搭建        项目部署:   (1) Eclipse中项目部署方法   (2) 使用Eclipse构建Maven ...

  6. 美团店铺评价语言处理以及文本分类(logistic regression)

    美团店铺评价语言处理以及分类(LogisticRegression) 第一篇 数据清洗与分析部分 第二篇 可视化部分, 第三篇 朴素贝叶斯文本分类 本文是该系列的第四篇 主要讨论逻辑回归分类算法的参数 ...

  7. spring面试大全

    一.spring如何实现资源管理? 使用 applicationContext.getResource(“classpath:文件名”):在src根目录下,在类路径下 applicationConte ...

  8. MySQLCouldn't find MySQL manager

    mysql出现MySQLCouldn't find MySQL manager错误的解决办法 最近我的一个linux服务器上的网站全部无法打开.....访问网站没有提示.....只出现该页无法显示的错 ...

  9. hdu5955 Guessing the Dice Roll【AC自动机】【高斯消元】【概率】

    含高斯消元模板 2016沈阳区域赛http://acm.hdu.edu.cn/showproblem.php?pid=5955 Guessing the Dice Roll Time Limit: 2 ...

  10. 使用nginx服务器如果遇到timeou情况时可以如下设置参数,使用fastcgi: fastcgi_connect_timeout 75; 链接 fastcgi_read_timeout 600; 读取 fastcgi_send_timeout 600; 发请求

    使用nginx服务器如果遇到timeou情况时可以如下设置参数,使用fastcgi: fastcgi_connect_timeout 75;  链接 fastcgi_read_timeout 600; ...