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

Nathaniel Smith njs at pobox.com
Thu Apr 7 15:47:14 EDT 2016


On Apr 7, 2016 8:57 AM, "Paul Moore" <p.f.moore at gmail.com> wrote:
[...]
> But the proposal for paths is to have a *specific* method that says
> "give me a string representing a filesystem path from this object". An
> "interpret this object as a string" wouldn't be appropriate for the
> cases where I'd want to do "give me a string representing a filesystem
> path". And that's where I get stuck, as I can't think of an example
> where I *would* want the more general option. For a number of reasons:
>
> 1. I can't think of a real-world example of when I'd *use* such a facility
> 2. I can't think of a real-world example of a type that might
> *provide* such a facility
> 3. I can't see how something so general would be of benefit.

Numpy and friends would implement this if it existed, for things like
converting numpy strings to python strings.

But I can't think of the cases where this would be useful either.

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? Most of the important ones are str methods, so
regular self-dispatch already handles it. Or I guess str.__add__(someobj),
but that's already handled by binop dispatch.

-n
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20160407/2e1afd7e/attachment.html>


More information about the Python-ideas mailing list