What is the proper way to determine if an object is a bytes-like object in Python?

后端 未结 7 1811
慢半拍i
慢半拍i 2021-01-30 09:40

I have code that expects str but will handle the case of being passed bytes in the following way:

if isinstance(data, bytes):
    data          


        
7条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-30 10:32

    >>> content = b"hello"
    >>> text = "hello"
    >>> type(content)
    
    >>> type(text)
    
    >>> type(text) is str
    True
    >>> type(content) is bytes
    True
    

提交回复
热议问题