[Python-ideas] __getattr__ bouncer for modules

Chris Angelico rosuav at gmail.com
Sat Apr 16 22:50:53 EDT 2016


Every now and then there's been talk of making it easier to subclass
modules, and the most common use case that I can remember hearing
about is descriptor protocol requiring code on the type. (For
instance, you can't change a module-level constant into a property
without moving away from the default ModuleType.)

How bad would it be for the default ModuleType to have a __getattr__
function which defers to the instance? Something like this:

def __getattr__(self, name):
    if '__getattr__' in self.__dict__:
        return self.__dict__['__getattr__'](name)
    raise AttributeError

The biggest downside I'm seeing is that module attributes double as
global names, which might mean this would get checked for every global
name that ends up being resolved from the builtins (which is going to
be a LOT). But I'm not sure if that's even true.

Dumb idea? Already been thought of and rejected?

ChrisA


More information about the Python-ideas mailing list