<p dir="ltr">On Apr 6, 2016 07:44, "Steven D'Aprano" <<a href="mailto:steve@pearwood.info">steve@pearwood.info</a>> wrote:<br>
><br>
> On Wed, Apr 06, 2016 at 11:30:32AM +0200, Petr Viktorin wrote:<br>
><br>
> > Python was in a similar situation with the .next method on iterators,<br>
> > which changed to __next__ in Python 3. PEP 3114 (which explains this<br>
> > change) says:<br>
> ><br>
> > > Code that nowhere contains an explicit call to a next method can<br>
> > > nonetheless be silently affected by the presence of such<br>
> > > a method. Therefore, this PEP proposes that iterators should have<br>
> > > a __next__ method instead of a next method (with no change in<br>
> > > semantics).<br>
> ><br>
> > How well does that apply to path/__path__?<br>
><br>
> I think it's potentially the same. Possibly there are fewer existing<br>
> uses of "obj.path" out there which conflict with this use, but there's<br>
> at least one in the std lib: sys.path.<br>
><br>
><br>
> > That PEP also introduced the next() builtin. This suggests that a<br>
> > protocol with __path__/__fspath__ would need a corresponding<br>
> > path()/fspath() builtin.<br>
><br>
> Not necessarily. Take a look at (say) dir(object()) and you'll see a few<br>
> dunders that don't correspond to built-ins:<br>
><br>
> __reduce__ and __reduce_ex__ are used by pickle;<br>
> __sizeof__ is used by sys.getsizeof;<br>
> __subclasshook__ is used by the ABC system;<br>
><br>
> Another example is __trunc__ used by math.trunc().<br>
><br>
> So any such fspath function should stand on its own as a useful<br>
> feature, not just because there's a dunder method __fspath__.</p>
<p dir="ltr">An even more precise analogy is provided by __index__, whose semantics are to provide safe casting to integer (the name is a historical accident), as opposed to __int__'s tendency to cast things to integer willy-nilly, including things that really shouldn't be silently accepted as integers. Basically __index__ is to __int__ as __(fs)path__ would be to __str__.</p>
<p dir="ltr">There's an operator.index but no builtins.index.</p>
<p dir="ltr">-n</p>