[Python-ideas] Working with Path objects: p-strings?
Serhiy Storchaka
storchaka at gmail.com
Sat Mar 26 15:29:34 EDT 2016
On 26.03.16 12:59, Andrew Barnert via Python-ideas wrote:
> Last year, everyone agreed that it would be good if at least the stdlib accepted paths everywhere, which might prompt third party libs to start doing the same. But nobody's started writing patches to do that. And I'm not sure we'd want it to happen in an uncoordinated way anyway, since there are at least four different ways to do it, and if we pick among them arbitrarily for different parts of the stdlib, we'll have a huge mess, and possibly behavioral inconsistencies.
There is an open issue and I'm working on the patch. I'm going to make
major path-related functions in the stdlib accepting the Path object in 3.6.
> The four ways I can think of are (in every function that currently takes a "path: str" argument, and should now take a "path: str | Path"):
>
> * path = str(path)
> * path = Path(path)
> * if isinstance(path, Path): ... else: ...
> * try: f = path.open('w') except AttributeError: open(path, 'w')
The official way:
try:
path = path.path
except AttributeError:
pass
The advantage is that this works not with concrete Path implementation,
but with any third-party Path class and other path-related objects like
DirEntry that provide the "path" attribute.
More information about the Python-ideas
mailing list