[Python-ideas] Better stdlib support for Path objects

Wolfgang Maier wolfgang.maier at biologie.uni-freiburg.de
Tue Oct 7 14:33:49 CEST 2014



On 10/06/2014 07:58 PM, Antoine Pitrou wrote:
> On Mon, 6 Oct 2014 13:47:14 -0400
> Barry Warsaw <barry-+ZN9ApsXKcEdnm+yROfE0A at public.gmane.org> wrote:
>>
>> One simple solution would be to sprinkle str() calls in various stdlib
>> methods, but I'm not sure if that would fail miserably in the face of bytes
>> paths (if the original stdlib API even accepts bytes paths).  The suggestion
>> in the issue is to add a "path protocol" and the referenced article suggests
>> .strpath and .bytespath.  OTOH, isn't str() and bytes() enough?
>
> str() works on anything (including enums and distutils compilers), so
> it's pretty bad IMO.
>

If that's a concern, couldn't that be solved with an abstract base class 
"Path" ? pathlib path classes could be inheriting from it, while modules 
like os, json and others that wish to accept also strings could register 
str, then call str() on all input that passes an isinstance(input, Path) 
check, raise an error otherwise.
The remaining question then would be where the abstract base class 
should live.

Wolfgang



More information about the Python-ideas mailing list