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

Nick Coghlan ncoghlan at gmail.com
Mon May 7 07:54:30 EDT 2018


On 7 May 2018 at 21:42, Nathaniel Smith <njs at pobox.com> wrote:

> On Mon, May 7, 2018, 03:45 Steven D'Aprano <steve at pearwood.info> wrote:
>
>> 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.
>>
>
> Well, yes, you do have to know the API to use it, and if you happen to
> have learned the os.path API but not the pathlib API then of course the
> os.path API will look more familiar. I'm not sure what this is supposed to
> prove.
>

I think it strongly suggests that *magically* introducing a path object
into a module's namespace would be a bad idea, since it harms readability
(since merely having `path` in the name isn't a strong enough hint that the
object in question is a `pathlib.Path` instance).

Your original point is still valid though: given the boilerplate reduction
already available via "from pathlib import Path; _this_dir =
Path(__file__).parent", it's the pathlib version that needs to be taken as
the baseline for how verbose the status quo really is, not the lower level
os.path API (no matter how accustomed some of us may still be to using the
latter).

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/1797fc68/attachment-0001.html>


More information about the Python-ideas mailing list