0

Any idea why this is happening when encoding is explicitly specified?

In [23]: import sys

In [24]: sys.getdefaultencoding()
Out[24]: 'utf-8'

In [25]: str(b'', encoding='utf-8') == ''
Out[25]: True

In [26]: str('') == ''
Out[26]: True

In [27]: str('', encoding='utf-8') == ''
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-27-e187972042f8> in <module>()
----> 1 str('', encoding='utf-8') == ''

TypeError: decoding str is not supported

According to the docs of str:

encoding defaults to sys.getdefaultencoding()

1
  • You seem to think that str('') decodes using the default encoding. It doesn't. It doesn't decode at all. Commented Feb 1, 2018 at 17:47

1 Answer 1

2

The help is pretty clear here:

str(object='') -> str

str(bytes_or_buffer[, encoding[, errors]]) -> str

Create a new string object from the given object. If encoding or errors is specified, then the object must expose a data buffer that will be decoded using the given encoding and error handler. Otherwise, returns the result of object.str() (if defined) or repr(object). encoding defaults to sys.getdefaultencoding(). errors defaults to 'strict'.

You can specify the encoding only for bytes (b'') not for strings such as ''.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.