[Python-ideas] __dir__ in which folder is this py file
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.
> On Sun, May 6, 2018, 1:54 AM Yuval Greenfield <ubershmekel at gmail.com>
>> 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.
>> 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
>> 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
>> * 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.
>> 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
>> Code of Conduct: http://python.org/psf/codeofconduct/
> Python-ideas mailing list
> Python-ideas at python.org
> Code of Conduct: http://python.org/psf/codeofconduct/
I would give +1 for __dirname__
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Python-ideas