Kevin Vasko
Kevin Vasko

Reputation: 1655

What does the errors parameter do in the built-in python bytes() class?

The documentation doesn't seem to describe what it does. https://docs.python.org/3/library/stdtypes.html#bytes

I'm assuming its a typical pattern used in other functions (e.g. bytesarray) as well but I can't figure out what it does and not sure what to google for.

Upvotes: 0

Views: 775

Answers (1)

glibdud
glibdud

Reputation: 7850

The description of bytes() begins with:

Firstly, the syntax for bytes literals is largely the same as that for string literals

Looking at str():

If at least one of encoding or errors is given, object should be a bytes-like object (e.g. bytes or bytearray). In this case, if object is a bytes (or bytearray) object, then str(bytes, encoding, errors) is equivalent to bytes.decode(encoding, errors).

bytes.decode() says:

errors may be given to set a different error handling scheme. The default for errors is 'strict', meaning that encoding errors raise a UnicodeError. Other possible values are 'ignore', 'replace' and any other name registered via codecs.register_error(), see section Error Handlers.

So the purpose of errors is to define how to handle encoding errors in the data. The aforementioned Error Handlers section contains more in-depth information.

Upvotes: 3

Related Questions