[Python-ideas] __getattr__ bouncer for modules
k7hoven at gmail.com
Sun Apr 17 04:49:53 EDT 2016
On Sun, Apr 17, 2016 at 7:06 AM, Random832 <random832 at fastmail.com> wrote:
> On Sat, Apr 16, 2016, at 23:42, Chris Angelico wrote:
>> It's not as clean as actually supporting @property, but it could be
>> done without the "bootstrap problem" of trying to have a module
>> contain the class that it's to be an instance of. All you have to do
>> is define __getattr__ as a regular top-level function, and it'll get
>> called. You can then dispatch to property functions if you wish (eg
>> """return globals()['_property_'+name]()"""), or just put all the code
>> straight into __getattr__.
> Or you could have ModuleType.__getattr(ibute?)__ search through some
> object other than the module itself for descriptors.
> from types import ModuleType, SimpleNamespace
> import sys
Not for a module, but for a *package*, this was reasonably easy (well,
at least after figuring out how ;-) to do in pre-3.5 Python: One would
import a submodule in __init__.py and then, within that submodule,
reconstruct the package using a subclass of ModuleType and replace the
original package in sys.modules.
More information about the Python-ideas