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

Ethan Furman ethan at stoneleaf.us
Thu Apr 7 14:59:41 EDT 2016


On 04/07/2016 11:45 AM, Chris Barker wrote:
> On Thu, Apr 7, 2016 at 11:28 AM, Chris Angelico wrote:

>> And this should make it easier for third-party code to be functional
>> without even being aware of the Path object. There are two basic
>> things that code will be doing with paths: passing them unchanged to
>> standard library functions (eg open()), and combining them with
>> strings. The first will work by definition; the second will if paths
>> can implicitly upcast to strings.
>
> so this is really a way to make the whole path!=string thing easier --
> we don't want to simply call str() on anything that might be a path,
> because that will work on anything, whether it's the least bit pathlike
> at all, but this would introduce a new kind of __str__, so that only
> things for which it makes sense would "Just work":
>
> str + something
>
> Would only concatenate to a new string if somethign was an object that
> couuld "losslessly" be considered a string?

Which is mildly attractive.

> but if we use the __index__ example, that is specifically "an integer
> that can be ued as an index", not "something that can be losslessly
> converted to an integer".

__index__ was originally created to support indexing, but has morphed 
over time to mean "something that can be losslessly converted to an 
integer".

--
~Ethan~


More information about the Python-ideas mailing list