[Python-Dev] Defining a path protocol

Paul Moore p.f.moore at gmail.com
Thu Apr 7 07:05:43 EDT 2016


On 7 April 2016 at 11:48, Nikolaus Rath <Nikolaus at rath.org> wrote:
> Why is:
>
> path = getattr(obj, '__fspath__') if hasattr(obj, '__fspath__') else obj
>
> better than
>
> path = str(obj) if isinstance(obj, pathlib.Path) else obj

One reason is that the former doesn't need you to import pathlib,
which is good if you need to work with older versions of Python that
don't have pathlib at all (yes, it's just some standard conditional
import boilerplate, but it's additional messiness).

Paul


More information about the Python-Dev mailing list