[Python-Dev] module customization

Nathaniel Smith njs at pobox.com
Thu Nov 16 01:12:30 EST 2017


On Wed, Nov 15, 2017 at 5:49 PM, Guido van Rossum <guido at python.org> wrote:
>> If not, why not, and if so, shouldn't PEP 562's __getattr__ also take a
>> 'self'?
>
> Not really, since there's only one module (the one containing the
> __getattr__ function). Plus we already have a 1-argument module-level
> __getattr__ in mypy. See PEP 484.

I guess the benefit of taking 'self' would be that it would make it
possible (though still a bit odd-looking) to have reusable __getattr__
implementations, like:

# mymodule.py
from auto_importer import __getattr__, __dir__

auto_import_modules = {"foo", "bar"}

# auto_importer.py
def __getattr__(self, name):
    if name in self.auto_import_modules:
        ...

-n

-- 
Nathaniel J. Smith -- https://vorpus.org


More information about the Python-Dev mailing list