bytes() 函数
bytes()函数用于创建一个新的不可变的字节序列对象,它是bytearray()的不可变版本。这个函数非常有用,尤其是在处理二进制数据、进行网络编程或者文件操作时。
语法
bytes([source[, encoding[, errors]]])
- source:可以是字符串、整数、可迭代对象或遵循缓冲区接口的对象。
- encoding和- errors:当- source是字符串时,必须指定- encoding参数,- bytes()会使用- str.encode()方法来将字符串转变成字节序列。
示例
print(bytes())  # 创建一个空的bytes对象
print(bytes("python", encoding='utf-8'))  # 使用字符串创建bytes对象
print(bytes(4))  # 创建一个长度为4,使用null字节填充的bytes对象
print(bytes([1, 2, 3]))  # 使用整数列表创建bytes对象
callable() 函数
callable()函数用于检查一个对象是否是可调用的。如果对象可以像函数一样被调用(例如函数、方法、类等),则返回True,否则返回False。
语法
callable(object)
- object:需要检查是否可调用的对象。
示例
def my_func():print("Hello, World!")print(callable(my_func))  # Trueclass MyClass:def __init__(self):print("Instance created")print(callable(MyClass))  # Trueobj = MyClass()
print(callable(obj))  # False,因为实例没有实现__call__方法