[Python-ideas] __dir__ in which folder is this py file
Steven D'Aprano
steve at pearwood.info
Mon May 7 06:44:36 EDT 2018
On Sun, May 06, 2018 at 09:33:03PM -0700, Nathaniel Smith wrote:
> How is
>
> data_path = __filepath__.parent / "foo.txt"
>
> more distracting than
>
> data_path = joinpath(dirname(__file__), "foo.txt")
Why are you dividing by a string? That's weird.
[looks up the pathlib docs]
Oh, that's why. It's still weird.
So yes, its very distracting.
First I have to work out what __filepath__ is, then I have to remember
the differences between all the various flavours of pathlib.<whatever>Path
and suffer a moment or two of existential dread as I try to work out
whether or not *this* specific flavour is the one I need. This might not
matter for heavy users of pathlib, but for casual users, it's a big,
intimidating API with:
- an important conceptual difference between pure paths and
concrete paths;
- at least six classes;
- about 50 or so methods and properties
As far as performance goes, I don't think it matters that we could
technically make pathlib imported lazily. Many people put all their
pathname manipulations at the beginning of their script, so lazy or not,
the pathlib module is going to be loaded *just after* startup, .
For many scripts, this isn't going to matter, but for those who want to
avoid the overhead of pathlib, making it lazy doesn't help. That just
delays the overhead, it doesn't remove it.
--
Steve
More information about the Python-ideas
mailing list