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

Ryan Gonzalez rymg19 at gmail.com
Thu Apr 7 10:42:32 EDT 2016


I can see this going horribly wrong when it comes to things like
concatenation. I've used JS far too many times to be excited for things
like this.

--
Ryan
[ERROR]: Your autotools build scripts are 200 lines longer than your
program. Something’s wrong.
http://kirbyfan64.github.io/
On Apr 7, 2016 8:05 AM, "Chris Angelico" <rosuav at gmail.com> wrote:

> This is a spin-off from the __fspath__ discussion on python-dev, in
> which a few people said that a more general approach would be better.
>
> Proposal: Objects should be allowed to declare that they are
> "string-like" by creating a dunder method (analogously to __index__
> for integers) which implies a loss-less conversion to str.
>
> This could supersede the __fspath__ "give me the string for this path"
> protocol, or could stand in parallel with it.
>
> Obviously str will have this dunder method, returning self. Most other
> core types (notably 'object') will not define it. Absence of this
> method implies that the object cannot be treated as a string.
>
> String methods will be defined as accepting string-like objects. For
> instance, "hello"+foo will succeed if foo is string-like.
>
> Downside: Two string-like objects may behave unexpectedly - foo+bar
> will concatenate strings if either is an actual string, but if both
> are other string-like objects, depends on the implementation of those
> objects.
>
> Bikeshedding:
>
> 1) What should the dunder method be named? __str_coerce__? __stringlike__?
>
> 2) Which standard types are sufficiently string-like to be used thus?
>
> 3) Should there be a bytes equivalent?
>
> 4) Should there be a format string "coerce to str"? "{}".format(x) is
> equivalent to str(x), but it might be nice to be able to assert that
> something's stringish already.
>
> Open season!
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20160407/87c216f8/attachment-0001.html>


More information about the Python-ideas mailing list