No, python doesn't support overloading because it doesn't need to. Python documentation often shows different ways of calling a method to illustrate different uses, but there is only one str()
callable (a type in this case).
In this case, str()
accepts multiple keyword arguments, which have default values if not specified. The str()
type then uses the those extra keyword arguments, if specified, to interpret a b''
byte string argument. If no keyword arguments were passed in, str()
behaves differently.
In other words, str()
adjusts it's behaviour based on wether or not the keyword arguments have been supplied. If that is the case and the first argument is a bytestring or bytearray, it'll decode that argument to unicode text, using the extra keyword arguments to control the decoding process.
You can define your own function that'll behave the same way as regards to the keyword arguments, checking the type of the first argument:
def f(o, encoding=None, errors=None):
if encoding is None and errors is None:
return o.__str__()
if isinstance(o, str):
raise TypeError('decoding str is not supported')
if not isinstance(o, (bytes, bytesarray)):
raise TypeError('coercing to str: need bytes, bytearray'
'or buffer-like object, %s found' % type(o).__name__)
return o.decode(encoding, errors)