Skip to main content

smart_bytes

Return a bytestring version of 's', encoded as specified in 'encoding'.

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

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

Return a bytestring version of 's', encoded as specified in 'encoding'. If strings_only is True, don't convert (some) non-string-like objects.

Parameters

NameTypeDescription
sAnyThe input value to be converted into a bytestring.
encodingstring = "utf-8"The character encoding to use for the conversion process.
strings_onlyboolean = falseIf True, prevents the conversion of non-string-like objects to ensure only text-based data is processed.
errorsstring = "strict"The error handling scheme to use for encoding errors, such as 'strict', 'ignore', or 'replace'.

Returns

TypeDescription
bytesThe encoded bytestring version of the input, or the original object if it is a lazy translation promise.