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

Greg Ewing greg.ewing at canterbury.ac.nz
Tue Oct 9 07:31:09 CEST 2012


Massimo DiPierro wrote:
> The + symbol means addition and union of disjoint sets. A path 
> (including a fs path) is a set of links (for a fs path, a link is a 
> folder name). Using the + symbols has a natural interpretation as 
> concatenation of subpaths (sets) to for form a longer path (superset).

A reason *not* to use '+' is that it would violate associativity
in some cases, e.g.

    (path + "foo") + "bar"

would not be the same as

    path + ("foo" + "bar")

Using '/', or any other operator not currently defined on strings,
would prevent this mistake from occuring.

A reason to want an operator is the symmetry of path concatenation.
Symmetrical operations deserve a symmetrical syntax, and to achieve
that in Python you need either an operator or a stand-alone function.

A reason to prefer an operator over a function is associativity.
It would be nice to be able to write

    path1 / path2 / path3

and not have to think about the order in which the operations are
being done.

If '/' is considered too much of a stretch, how about '&'? It
suggests a kind of addition or concatenation, and in fact is
used for string concatenation in some other languages.

-- 
Greg



More information about the Python-ideas mailing list