Skip to main content

force_bytes

Similar to smart_bytes, except that lazy instances are resolved to strings, rather than kept as lazy objects.

If strings_only is True, don't convert (some) non-string-like objects.

def force_bytes(
s: Any,
encoding: string = "utf-8",
strings_only: boolean = false,
errors: string = "strict"
) - > bytes

Similar to smart_bytes, except that lazy instances are resolved to strings, rather than kept as lazy objects. If strings_only is True, don't convert (some) non-string-like objects.

Parameters

NameTypeDescription
sAnyThe input value to be converted into bytes, such as a string, memoryview, or lazy object.
encodingstring = "utf-8"The character encoding to use when converting strings to bytes.
strings_onlyboolean = falseWhen True, prevents the conversion of non-string-like objects (such as integers or None) to bytes.
errorsstring = "strict"The error handling scheme to use for encoding and decoding errors.

Returns

TypeDescription
bytesThe byte-string representation of the input value, encoded according to the specified encoding.