[Python-ideas] PEP 428 - object-oriented filesystem paths

Antoine Pitrou solipsis at pitrou.net
Fri Oct 5 21:41:01 CEST 2012


On Fri, 5 Oct 2012 23:16:25 +0400
Oleg Broytman <phd at phdru.name> wrote:
> Hi!
> 
> On Fri, Oct 05, 2012 at 08:25:34PM +0200, Antoine Pitrou <solipsis at pitrou.net> wrote:
> > This PEP proposes the inclusion of a third-party module, `pathlib`_, in
> > the standard library.
> 
>    +1 from me for a sane path handling in the stdlib!
> 
> >     >>> p = Path('/home/antoine/pathlib/setup.py')
> >     >>> p.name
> >     'setup.py'
> >     >>> p.ext
> >     '.py'
> >     >>> p.root
> >     '/'
> >     >>> p.parts
> >     <PosixPath.parts: ['/', 'home', 'antoine', 'pathlib', 'setup.py']>
> >     >>> list(p.parents())
> >     [PosixPath('/home/antoine/pathlib'), PosixPath('/home/antoine'), PosixPath('/home'), PosixPath('/')]
> 
>    Some attributes are properties and some are methods. Which is which?
> Why .root is a property but .parents() is a method? .owner/.group are
> properties but .exists() is a method, and so on. .stat() just returns
> self._stat, but said ._stat is a property!

parents() returns a generator (hence the list() call in the
example above). A generator-returning property sounds a bit too
confusing IMHO.

._stat is an implementation detail.  stat() and exists() both
mirror similar APIs in the os / os.path modules.

.name, .ext, .root, .parts just return static, immutable properties of
the path, I see no reason for them to be methods.

> >   A Windows UNC path (e.g.
> >   ``\\some\\share\\myfile.txt``) always has a drive and a root
> >   (here, ``\\some\\share`` and ``\\``, respectively).
> 
>    If I understand it correctly these should are either
> \\\\some\\share\\myfile.txt and \\\\some\\share
>    or
> \\some\share\myfile.txt and \\some\share
>    no?

Ah, right. I'll correct it.

Thanks

Antoine.


-- 
Software development and contracting: http://pro.pitrou.net





More information about the Python-ideas mailing list