Clarify why pathlib doesn't inherit from str

This commit is contained in:
Brett Cannon 2016-04-22 11:45:53 -07:00
parent aee296ace8
commit c26ed6d453
1 changed files with 10 additions and 2 deletions

View File

@ -156,8 +156,16 @@ contrasts with some other Path class proposals which were derived from
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