With PEP 562, the name __dir__ is off limits for this.CodyOn Sun, May 6, 2018, 1:54 AM Yuval Greenfield <ubershmekel@gmail.com> wrote:______________________________Hi Ideas,I often need to reference a script's current directory. I end up writing:import osSRC_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/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 GreenfieldP.s. nodejs has it - https://nodejs.org/docs/latest/api/modules.html# also I apologize if this has been suggested before - my googling didn't find a previous thread.modules_dirname _________________
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/
_______________________________________________
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/