[Python-ideas] PEP 428 - object-oriented filesystem paths

Ethan Furman ethan at stoneleaf.us
Fri Oct 5 22:06:57 CEST 2012

Antoine Pitrou wrote:
> On Fri, 5 Oct 2012 20:19:12 +0100
> Paul Moore <p.f.moore at gmail.com> wrote:
>> On 5 October 2012 19:25, Antoine Pitrou <solipsis at pitrou.net> wrote:
>>> A path can be joined with another using the ``__getitem__`` operator::
>>>     >>> p = PurePosixPath('foo')
>>>     >>> p['bar']
>>>     PurePosixPath('foo/bar')
>>>     >>> p[PurePosixPath('bar')]
>>>     PurePosixPath('foo/bar')
>> There is a risk that this is too "cute". However, it's probably better
>> than overloading the '/' operator, and you do need something short.
> I think overloading '/' is ugly (dividing paths??).

But '/' is the normal path separator, so it's not dividing; and it 
certainly makes more sense than `%` with string interpolations.  ;)

> Someone else proposed overloading '+', which would be confusing since we
> need to be able to combine paths and regular strings, for ease of use.
> The point of using __getitem__ is that you get an error if you replace
> the Path object with a regular string by mistake:
>>>> PurePath('foo')['bar']
> PurePosixPath('foo/bar')
>>>> 'foo'['bar']
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> TypeError: string indices must be integers
> If you were to use the '+' operator instead, 'foo' + 'bar' would work
> but give you the wrong result.

I would rather use the `/` and `+` and risk the occasional wrong result. 
(And yes, I have spent time tracking bugs because of that wrong result 
when using my own Path module -- and I'd still rather make that trade-off.)


More information about the Python-ideas mailing list