[Python-ideas] PEP 428: poll about the joining syntax

Victor Stinner victor.stinner at gmail.com
Tue Oct 9 11:33:08 CEST 2012


> Since there has been some controversy about the joining syntax used in
> PEP 428 (filesystem path objects), I would like to run an informal poll
> about it. Please answer with +1/+0/-0/-1 for each proposal:
>
> - `p[q]` joins path q to path p
> - `p + q` joins path q to path p
> - `p / q` joins path q to path p
> - `p.join(q)` joins path q to path p

I cannot decide with such trivial examples. More realistic examples:
---
def read_config(name):
  home = Path(os.path.expanduser("~")) # pathlib doesn't support expanduser??
  with open(home / ".config" / name + ".conf") as f:
      return fp.read()
---

The join() method has an advantage: it avoids temporary objects
(config / ".config" is my example).
---
def read_config(name):
  home = Path(os.path.expanduser("~")) # pathlib doesn't support expanduser??
  with open(home.join(".config", name + ".conf")) as f:
      return fp.read()
---

It should work even if name is a Path object, so Path + str should
concatenate a suffix without adding directory separator.

My vote:


> - `p[q]` joins path q to path p

home[".config"][name] # + ".conf" ???

-1

> - `p + q` joins path q to path p

home + ".config" + name # + ".conf" ???

-1 -> Path + str must be reserved to add a suffix

> - `p / q` joins path q to path p

home / ".config" / name + ".conf"

+1: it's natural, but maybe "suboptimal" in performance

> - `p.join(q)` joins path q to path p

home.join(".config", name + ".conf")

+0: more efficient, but it may be confusing with str.join() which is
very different.

a.join(b, c) : a is the separator or the root directory, depending on
the type of a (str or Path).

We should avoid confusion between Path and str methods and operator
(a+b and a.join(b)).

Victor



More information about the Python-ideas mailing list