[Python-ideas] __getattr__ bouncer for modules

Random832 random832 at fastmail.com
Sat Apr 16 23:26:44 EDT 2016


On Sat, Apr 16, 2016, at 22:50, Chris Angelico wrote:
> 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.

It is not. (Also, incidentally, defining a global called __class__ does
not set the module's class.)

I don't think this would be enough alone to let you use property
decorators on a module - you'd have to explicitly define a __getattr__
(and __setattr__). And of course make sure that the names you're using
as properties don't exist as real members of the module, since you're
using __getattr__ instead of __getattribute__.


More information about the Python-ideas mailing list