<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">2018-05-06 15:28 GMT+02:00 Cody Piersall <span dir="ltr"><<a href="mailto:cody.piersall@gmail.com" target="_blank">cody.piersall@gmail.com</a>></span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="auto"><div>With PEP 562, the name __dir__ is off limits for this.</div><span class="HOEnZb"><font color="#888888"><div dir="auto"><br></div></font></span><div dir="auto"><span class="HOEnZb"><font color="#888888">Cody<br><br></font></span><div class="gmail_quote" dir="auto"><div><div class="h5"><div dir="ltr">On Sun, May 6, 2018, 1:54 AM Yuval Greenfield <<a href="mailto:ubershmekel@gmail.com" target="_blank">ubershmekel@gmail.com</a>> wrote:<br></div></div></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div class="h5"><div dir="ltr">Hi Ideas,<div><br></div><div>I often need to reference a script's current directory. I end up writing:</div><div><br></div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div>import os</div><div>SRC_DIR = os.path.dirname(__file__)</div></blockquote><div><br></div><div>

<div style="color:rgb(34,34,34);font-family:sans-serif;font-size:13px;font-style:normal;font-variant-ligatures:normal;font-variant-caps:normal;font-weight:400;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration-style:initial;text-decoration-color:initial">But I would prefer to have a new dunder for that. I propose: "__dir__". 

<span style="color:rgb(34,34,34);font-family:sans-serif;font-size:13px;font-style:normal;font-variant-ligatures:normal;font-variant-caps:normal;font-weight:400;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;background-color:rgb(255,255,255);text-decoration-style:initial;text-decoration-color:initial;float:none;display:inline">I was wondering if others would find it convenient to include such a shortcut.<span> </span></span>

</div>

</div><div><br></div><div>Here are some examples of dirname(__file__) in prominent projects.</div><div><br></div><div><a href="https://github.com/tensorflow/models/search?l=Python&q=dirname&type=" rel="noreferrer" target="_blank">https://github.com/tensorflow/<wbr>models/search?l=Python&q=<wbr>dirname&type=</a><br></div><div><a href="https://github.com/django/django/search" rel="noreferrer" target="_blank">https://github.com/django/<wbr>django/search</a><span style="color:rgb(34,34,34);font-family:sans-serif;font-size:13px;font-style:normal;font-variant-ligatures:normal;font-variant-caps:normal;font-weight:400;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;background-color:rgb(255,255,255);text-decoration-style:initial;text-decoration-color:initial;float:none;display:inline">?l=Python&q=<wbr>dirname&type=</span><br></div><div><span style="text-align:start;text-indent:0px;background-color:rgb(255,255,255);text-decoration-style:initial;text-decoration-color:initial;float:none;display:inline"><a href="https://github.com/nose-devs/nose/search" rel="noreferrer" target="_blank">https://github.com/nose-devs/<wbr>nose/search</a><span style="color:rgb(34,34,34);font-family:sans-serif;font-size:13px;font-style:normal;font-variant-ligatures:normal;font-variant-caps:normal;font-weight:400;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;background-color:rgb(255,255,255);text-decoration-style:initial;text-decoration-color:initial;float:none;display:inline">?l=Python&q=<wbr>dirname&type=</span>

<br></span></div><div><br></div><div>Reasons not to add __dir__:<br></div><div>* There already is one way to do it and it's clear and fairly short.</div><div>* Avoid the bikeshed discussion of __dir__, __folder__, and other candidates.</div><div><br></div><div>Reasons to add it:</div><div>* 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.</div><div>* I would save about 20 characters and a line from 50% of my python scripts.</div><div>* 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.</div><div><br></div><div>Cheers,</div><div><br></div><div>Yuval Greenfield</div><div><br></div><div>P.s. nodejs has it - <a href="https://nodejs.org/docs/latest/api/modules.html#modules_dirname" rel="noreferrer" target="_blank">https://nodejs.org/docs/<wbr>latest/api/modules.html#<wbr>modules_dirname</a> also I apologize if this has been suggested before - my googling didn't find a previous thread.</div><div><br></div></div></div></div><span class="">
______________________________<wbr>_________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org" rel="noreferrer" target="_blank">Python-ideas@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-ideas" rel="noreferrer noreferrer" target="_blank">https://mail.python.org/<wbr>mailman/listinfo/python-ideas</a><br>
Code of Conduct: <a href="http://python.org/psf/codeofconduct/" rel="noreferrer noreferrer" target="_blank">http://python.org/psf/<wbr>codeofconduct/</a><br>
</span></blockquote></div></div></div>
<br>______________________________<wbr>_________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org">Python-ideas@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-ideas" rel="noreferrer" target="_blank">https://mail.python.org/<wbr>mailman/listinfo/python-ideas</a><br>
Code of Conduct: <a href="http://python.org/psf/codeofconduct/" rel="noreferrer" target="_blank">http://python.org/psf/<wbr>codeofconduct/</a><br>
<br></blockquote></div><br></div><div class="gmail_extra"><br></div><div class="gmail_extra">Hi,</div><div class="gmail_extra"><br></div><div class="gmail_extra">I would give +1 for __dirname__</div><div class="gmail_extra"><br></div><div class="gmail_extra">George</div></div>