python bytes_Python bytes()

python bytes

Python bytes() is a built-in function. This function returns bytes object that is an immutable sequence of integers in the range 0 <= x < 256.

Python bytes()是一个内置函数。 此函数返回字节对象,该对象是不可变的整数序列,范围为0 <= x <256。

Python bytes() (Python bytes())

Python bytes() function syntax is:

Python bytes()函数语法为:

class bytes(]])

source is used to initialize the bytes object. This is an optional argument.

source用于初始化bytes对象。 这是一个可选参数。

encoding is optional unless source is string. It’s used to convert the string to bytes using str.encode() function.

除非source是string,否则encoding是可选的。 它用于使用str.encode()函数将字符串转换为字节。

errors is optional parameter. It’s used if the source is string and encoding fails due to some error.

errors是可选参数。 如果源是字符串并且由于某些错误而编码失败,则使用它。

There are some specific rules followed by bytes() function depending on the type of source.

根据源类型,有一些特定的规则,后跟bytes()函数。

  • If no argument is passed, empty bytes object is returned.

    如果未传递任何参数,则返回空字节对象。
  • If source is integer, it initializes the bytes object with array of given length with null values.

    如果source是整数,它将使用具有空值的给定长度的数组初始化bytes对象。
  • If source is string, encoding is mandatory and it’s used to convert string to byte array.

    如果source是字符串,则编码是强制性的,用于将字符串转换为字节数组。
  • If source is iterable, such as list, it must be an iterable of integers in the range 0 <= x < 256, which are used as the initial contents of the array.

    如果source是可迭代的,例如list ,则它必须是0 <= x <256范围内的整数的可迭代对象,这些整数用作数组的初始内容。

Overall bytes() function is very similar to bytearray() function.

总体而言,bytes()函数与bytearray()函数非常相似。

Let’s look at some of the examples of bytes() function.

让我们看一下bytes()函数的一些示例。

没有参数的bytes() (bytes() with no arguments)

b = bytes()
print(b)

Output: b''

输出: b''

具有字符串和不变性的bytes() (bytes() with string and immutability)

# string to bytes
# encoding is mandatory, otherwise "TypeError: string argument without an encoding"
b = bytes('abc', 'UTF-8')
print(b)

# Below code will throw error:
# TypeError: 'bytes' object does not support item assignment
# b[1] = 65  # immutable

Output: b'abc'

输出: b'abc'

Note that if we try to change the element value in the bytes object, we will get an error message as shown in the above comment.

请注意,如果尝试更改bytes对象中的元素值,则会收到一条错误消息,如上面的注释所示。

具有int参数的bytes() (bytes() with int argument)

# bytes of given size, elements initialized to null
b = bytes(5)
print(b)

Output: b'\x00\x00\x00\x00\x00'

输出: b'\x00\x00\x00\x00\x00'

具有可迭代的bytes() (bytes() with iterable)

# bytes from iterable
b = bytes([1, 2, 3])
print(b)

Output: b'\x01\x02\x03'

输出: b'\x01\x02\x03'

That’s all for a quick guide of python bytes() function.

这就是python bytes()函数的快速指南。

GitHub Repository. GitHub存储库中检出完整的python脚本和更多Python示例。

Reference: Official Documentation

参考: 官方文档

翻译自: https://www.journaldev.com/22747/python-bytes

python bytes

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值