[Python-ideas] Dunder method to make object str-like

Random832 random832 at fastmail.com
Thu Apr 7 16:04:50 EDT 2016


On Thu, Apr 7, 2016, at 15:47, Nathaniel Smith wrote:
> The reason that it's useful to have __index__, and that it would be
> useful
> to have __fspath__, is that in both cases there are a bunch of interfaces
> defined as part of the core language that need to implement the consumer
> side of the protocol, so a core language protocol is the only real way to
> go. I'm not thinking of a lot of analogous APIs in core python take
> specifically-strings?

Well, there's getattr and the like. I'm not sure why you'd want to pass
a not-really-a-string (though, an ASCII bytes is the one thing I _can_
think of*, and didn't the inability to pass unicode strings to a bunch
of APIs cause no end of trouble for Python 2 users?), but then I'm not
sure why you'd want to pass anything but an int (or a python 2 int/long)
to list indexing.

*Maybe a RUE-string that subclasses bytes and uses UTF-8.


More information about the Python-ideas mailing list