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

George Fischhof george at fischhof.hu
Sun May 6 11:05:02 EDT 2018


2018-05-06 15:28 GMT+02:00 Cody Piersall <cody.piersall at gmail.com>:

> With PEP 562, the name __dir__ is off limits for this.
>
> Cody
>
> On Sun, May 6, 2018, 1:54 AM Yuval Greenfield <ubershmekel at gmail.com>
> wrote:
>
>> Hi Ideas,
>>
>> I often need to reference a script's current directory. I end up writing:
>>
>> import os
>> SRC_DIR = os.path.dirname(__file__)
>>
>>
>> But I would prefer to have a new dunder for that. I propose: "__dir__".  I
>> was wondering if others would find it convenient to include such a shortcut.
>>
>>
>> Here are some examples of dirname(__file__) in prominent projects.
>>
>> https://github.com/tensorflow/models/search?l=Python&q=dirname&type=
>> https://github.com/django/django/search?l=Python&q=dirname&type=
>> https://github.com/nose-devs/nose/search?l=Python&q=dirname&type=
>>
>> Reasons not to add __dir__:
>> * There already is one way to do it and it's clear and fairly short.
>> * Avoid the bikeshed discussion of __dir__, __folder__, and other
>> candidates.
>>
>> Reasons to add it:
>> * os.path.dirname(__file__) returns the empty string when you're in the
>> same directory as the script. Luckily, os.path.join understands an empty
>> string as a ".", but this still is suboptimal for logging where it might be
>> surprising to find the empty string. __dir__ could be implemented to
>> contain a "." in that case.
>> * I would save about 20 characters and a line from 50% of my python
>> scripts.
>> * This is such a common construct that everyone giving it their own name
>> seems suboptimal for communicating. Common names include: here, path,
>> dirname, module_dir.
>>
>> Cheers,
>>
>> Yuval Greenfield
>>
>> P.s. nodejs has it - https://nodejs.org/docs/latest/api/modules.html#
>> modules_dirname also I apologize if this has been suggested before - my
>> googling didn't find a previous thread.
>>
>> _______________________________________________
>> Python-ideas mailing list
>> Python-ideas at python.org
>> https://mail.python.org/mailman/listinfo/python-ideas
>> Code of Conduct: http://python.org/psf/codeofconduct/
>>
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
>

Hi,

I would give +1 for __dirname__

George
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180506/468aa421/attachment.html>


More information about the Python-ideas mailing list