[Python-ideas] tweaking the file system path protocol
Koos Zevenhoven
k7hoven at gmail.com
Sun May 28 10:35:38 EDT 2017
On Sun, May 28, 2017 at 9:15 AM, Nick Coghlan <ncoghlan at gmail.com> wrote:
>
> However, if we *did* make such a change, it should also be made for
> operator.index as well, since that is similarly inconsistent with the
> way the int/float/etc constructor protocols work:
>
Part of this discussion seems to consider consistency as the only
thing that matters, but consistency is only the surface here. I won't
comment on the __index__ issue, and especially not call it a
"misfeature", because I haven't thought about it deeply, and my
comments on it would be very shallow. I might ask about it though,
like the OP did.
Don't get me wrong, I like consistency very much. But regarding the
__fspath__ case, there are not that many people *writing*
fspath-enabled classes. Instead, there are many many many more people
*using* such classes (and dealing with their compatibility issues in
different ways). For those people, the current behavior brings
consistency---after all, it was of course designed by thinking about
it from all angles and not just based on my or anyone else's own use
cases only.
-- Koos
> >>> from operator import index
> >>> class MyInt(int):
> ... def __int__(self):
> ... return 5
> ... def __index__(self):
> ... return 5
> ...
> >>> int(MyInt(10))
> 5
> >>> index(MyInt(10))
> 10
> >>> class MyFloat(float):
> ... def __float__(self):
> ... return 5.0
> ...
> >>> float(MyFloat(10))
> 5.0
> >>> class MyComplex(complex):
> ... def __complex__(self):
> ... return 5j
> ...
> >>> complex(MyComplex(10j))
> 5j
> >>> class MyStr(str):
> ... def __str__(self):
> ... return "Hello"
> ...
> >>> str(MyStr("Not hello"))
> 'Hello'
> >>> class MyBytes(bytes):
> ... def __bytes__(self):
> ... return b"Hello"
> ...
> >>> bytes(MyBytes(b"Not hello"))
> b'Hello'
>
> Regards,
> Nick.
>
--
+ Koos Zevenhoven + http://twitter.com/k7hoven +
More information about the Python-ideas
mailing list