[Python-checkins] peps: Clarify why pathlib doesn't inherit from str

brett.cannon python-checkins at python.org
Fri Apr 22 14:45:58 EDT 2016


https://hg.python.org/peps/rev/9298dc5eeecc
changeset:   6291:9298dc5eeecc
user:        Brett Cannon <brett at python.org>
date:        Fri Apr 22 11:45:53 2016 -0700
summary:
  Clarify why pathlib doesn't inherit from str

files:
  pep-0428.txt |  12 ++++++++++--
  1 files changed, 10 insertions(+), 2 deletions(-)


diff --git a/pep-0428.txt b/pep-0428.txt
--- a/pep-0428.txt
+++ b/pep-0428.txt
@@ -156,8 +156,16 @@
 if you want a path to act as a sequence, you have to lookup a dedicated
 attribute (the ``parts`` attribute).
 
-Not behaving like one of the basic builtin types also minimizes the potential
-for confusion if a path is combined by accident with genuine builtin types.
+The key reasoning behind not inheriting from ``str`` is to prevent confusing
+any object that has a string representation -- which is all Python objects --
+with a path object. This avoids situations when people must interface with an
+API that mandates a string representation for a file system path, tempting you
+to use ``str(path)`` to extract the string representation of the path. This
+would be a problem if ``path = None; open(str(path), 'w')``. A `blog post`_ by
+a Python core developer goes into more detail on the reasons behind this
+specific design decision.
+
+.. _blog post: http://www.snarky.ca/why-pathlib-path-doesn-t-inherit-from-str
 
 
 Immutability

-- 
Repository URL: https://hg.python.org/peps


More information about the Python-checkins mailing list