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

Chris Angelico rosuav at gmail.com
Sun May 6 13:44:07 EDT 2018

On Mon, May 7, 2018 at 1:05 AM, George Fischhof <george at fischhof.hu> wrote:
>> 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__)
> I would give +1 for __dirname__

Something to keep in mind: making this available to every module,
whether it's wanted or not, means that the Python interpreter has to
prepare that just in case it's wanted. That's extra work as part of
setting up a module. Which, in turn, means it's extra work for EVERY
import, and consequently, slower Python startup. It might only be a
small slowdown, but it's also an extremely small benefit.



More information about the Python-ideas mailing list