[Python-ideas] os.path.commonprefix: Yes that old chestnut.

Paul Moore p.f.moore at gmail.com
Tue Mar 24 11:05:17 CET 2015


On 24 March 2015 at 01:54,  <random832 at fastmail.us> wrote:
> On Mon, Mar 23, 2015, at 18:48, Paul Moore wrote:
>> The type of the return value should
>> be a concrete type - probably type(p1)?
>
> I'd argue it should be the common supertype, so a PurePosixPath if both
> are posix and one is pure, a Path or PurePath if one is windows and the
> other is posix.

That's not really possible in the face of the possibility that an
argument could be a user-defined class, possibly not even a
pathlib.Path subclass (given duck typing). That's a clear benefit of a
Path method, actually - the type of the return value is easy to
specify - it's the type of self.

Actually, in many ways, this is really a list (sequence) method -
common_prefix - applied to the "parts" property of a Path. It's a
shame there isn't a sequence utils module in the stdlib...

One thing my implementation doesn't (yet) handle is case sensitivity.
The common prefix of WindowsPath('c:\\FOO\\bar') and
WindowsPath('C:\\Foo\\BAR') should be WindowsPath('C:\\Foo'). But not
for PosixPath. (And again, when they are mixed, which is silly but
possible, what behaviour should apply? "Work like self" is the obvious
answer if we have a method).

Paul


More information about the Python-ideas mailing list