[Python-ideas] Making pathlib paths inherit from str

Chris Angelico rosuav at gmail.com
Wed Apr 6 13:27:31 EDT 2016

On Thu, Apr 7, 2016 at 2:04 AM, Koos Zevenhoven <k7hoven at gmail.com> wrote:
> Rationale
> ---------
> Furthermore, functions now
> returning instances of ``str`` to represent paths could in future
> versions return path objects, with only minor backwards-incompatibility
> worries.
> Making ``pathlib`` classes subclasses of ``str``
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> Overriding all ``str``-specific methods
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> The perhaps unusual practice of disabling most base-class
> methods can be regarded as being conservative in adding ``str``
> functionality to path objects.

I'm not sure I entirely understand what's going on here. Your
rationale is "it should be possible to use a Path as a str", and
that's supported by your proposal to subclass str; but then you want
to override a bunch of methods to force users to be aware that a Path
is *not* a str. Why subclass only to force people to distinguish?

If you want to make a Path act "just a little bit" like a str, I'd
expect to go the other way: don't subclass str, and add in a specific
set of methods to provide str-like functionality. Or am I missing
something here?


More information about the Python-ideas mailing list