[Python-ideas] __dir__ in which folder is this py file

Nick Coghlan ncoghlan at gmail.com
Sun May 6 23:29:11 EDT 2018


On 7 May 2018 at 12:35, Chris Angelico <rosuav at gmail.com> wrote:

> On Mon, May 7, 2018 at 12:13 PM, Nick Coghlan <ncoghlan at gmail.com> wrote:
> > So I have a different suggestion: perhaps it might make sense to propose
> > promoting a key handful of path manipulation operations to the status of
> > being builtins?
> >
> > Specifically, the ones I'd have in mind would be:
> >
> > - dirname (aka os.path.dirname)
> > - joinpath (aka os.path.join)
>
> These two are the basics of path manipulation. +1 for promoting to
> builtins, unless pathlib becomes core (which I suspect isn't
> happening).
>

pathlib has too many dependencies to ever make the type available as a
builtin:

    $ ./python -X importtime -c pass 2>&1 | wc -l
    25
    $ ./python -X importtime -c "import pathlib" 2>&1 | wc -l
    53

It's a good way of unifying otherwise scattered standard library APIs, but
it's overkill if all you want to do is to calculate and resolve some
relative paths.


> > - abspath (aka os.path.abspath)
>
> Only +0.5 on this, as it has to do file system operations. It may be
> worthwhile, instead, to promote os.path.normpath, which (like the
> others) is purely processing the string form of the path. It'll return
> the same value regardless of the file system.
>

My rationale for suggesting abspath() over any of its component parts is
based on a few key considerations:

- "make the given path absolute" is a far more common path manipulation
activitity than "normalise the given path" (most users wouldn't even know
what the latter means - the only reason *I* know what it means is because I
looked up the docs for abspath while writing my previous comment)
- __file__ isn't always absolute (especially in __main__), so you need to
be able to do abspath(__file__) in order to reliably apply dirname() more
than once
- it can stand in for both os.getcwd() (when applied to the empty string or
os.curdir) and os.path.normpath() (when the given path is already
absolute), so we get 3 new bits of builtin functionality for the price of
one new builtin name
- I don't want to read "normpath(joinpath(getcwd(), relpath))" when I could
be reading "abspath(relpath)" instead

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180507/b344b1d0/attachment.html>


More information about the Python-ideas mailing list