Re: [Python-Dev] Defining a path protocol

2016年4月07日 01:01:37 -0700

On 6 April 2016 at 23:46, Brett Cannon <[email protected]> wrote:
> str(path) will definitely work, path.__path__ will work if you're running
> the next set of bugfix releases. fspath(path) will only work in Python 3.6
> and newer.
Ah, that was something I hadn't appreciated, that the builtin would be
3.6+ whereas the protocol would be added to current bugfix releases.
>> Maybe a compatibility library could
>> add
>>
>> try:
>> fspath
>> except NameError:
>> try:
>> import pathlib
>> def fspath(p):
>> if isinstance(p, pathlib.Path):
>> return str(p)
>> return p
>> except ImportError:
>> def fspath(p):
>> return p
>>
>> It's messy, like all compatibility code, but it allows code to use
>> fspath(p) in older versions.
>
>
> I would tweak it to check for __fspath__ before it resorted to calling
> str(), but yes, that could be something people use.
Yeah, the above code assumes that if the builtin isn't available, nor
will the protocol be (see my misunderstanding above).
Paul
_______________________________________________
Python-Dev mailing list
[email protected]
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com

Reply via email to