0

As far as I know, Python doesn't support function overloading, but in Python documentation

link to py3k doc

seems that there are 2 different str methods, str(object='') and str(object=b'', encoding='utf-8', errors='strict')

How are defined these functions? When is the first invoked and when the second?

Can I create my own f(o='') and f(o=b'') functions (makes sense)?

UPDATE

Output for str(b'abc','utf-8') is abc but

output for str(b'abc') is b'abc'

BenMorel
  • 34,448
  • 50
  • 182
  • 322
José Luis
  • 3,713
  • 4
  • 33
  • 37

2 Answers2

8

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)
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Your fuction example is not the str function behavior in python3 output for `str(b'abc','utf-8')` is `abc` but output for `str(b'abc')` is `b'abc'` – José Luis Nov 28 '12 at 17:35
  • @JoseLuis: I remembered it wrong. Updated the answer to better reflect what `str()` does. The same principle applies though. – Martijn Pieters Nov 28 '12 at 18:31
  • @JoseLuis: I've referenced the C implementation of `str()` and the `f` function is a good approximation in Python of what it does now. Good enough for illustration purposes in any case. – Martijn Pieters Nov 28 '12 at 19:01
  • ok, that means that py3k documentation is wrong, or at least not clear, in two points. There is only one str() function (and no two), and the function definition is `str(object='', encoding=None, errors='strict')` (and not `str(object=b'', encoding='utf-8', errors='strict')` ) – José Luis Nov 28 '12 at 19:46
  • @JoseLuis: The default encoding for conversion *is* UTF-8; see http://docs.python.org/3/library/stdtypes.html#bytes.decode and http://docs.python.org/3/howto/unicode.html, and as I stated in my answer, the two behaviours are separate and thus *documented* distinctly. Don't confuse a misunderstanding about how the documentation tries to provide clarity with being wrong. – Martijn Pieters Nov 28 '12 at 20:38
  • Where can I find the C implementation of str()? I downloaded the Python source code, but I don't how is structured and I can't find it – José Luis Nov 28 '12 at 23:10
  • `Objects/unicodeobject.c`, use the [C API](http://docs.python.org/3/c-api/index.html) as a guide; constructor is `unicode_new()`. – Martijn Pieters Nov 29 '12 at 07:39
  • @JoseLuis: The C API has to do more work to parse arguments, but it doesn't do it quite like what you used in the suggested edit. You'll need to study the C API some more, I think. – Martijn Pieters Nov 29 '12 at 14:43
0

No, there's only one str().

It is shown twice for the sake of clarity (that is, to demonstrate different use cases).

You can achieve similar effects in your code by using default arguments, positional argument and keyword arguments. See Python normal arguments vs. keyword arguments

Community
  • 1
  • 1
NPE
  • 486,780
  • 108
  • 951
  • 1,012