[Python-Dev] file system path protocol PEP

Nick Coghlan ncoghlan at gmail.com
Sat May 14 08:24:04 EDT 2016


On 14 May 2016 at 19:36, Sven R. Kunze <srkunze at mail.de> wrote:
> Fine example. Thinking naively, I would say, when somebody made an effort to
> write __fspath__, it should be respected. Maybe, that's just me.

Over time, we've generally moved towards treating subclasses as if
they were the base class where protocols are concerned, and
encouraging the use of composition if people don't actually want that
"is-a" behaviour.

For example, compare the int protocol to the last major type protocol
we added, index:

>>> from operator import index
>>> class MyInt(int):
...     def __int__(self):
...         return 42
...     def __index__(self):
...         return 42
...
>>> x = MyInt(5)
>>> x
5
>>> int(x)
42
>>> index(x)
5

In the case of retroactively added protocols like operator.index() and
os.fspath(), where inheritance from the relevant builtin type was
previously the only option, retaining that behaviour is also the only
way to avoid a performance regression for those subclasses.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia


More information about the Python-Dev mailing list