Brett Cannon added the comment:
While seing if it was worth making isinstance work with super(), I came up with
this at Antoine's suggestion of using a proxy instead of a mixin:
class LazyProxy(importlib.abc.Loader):
def __init__(self, loader):
self.loader = loader
def __call__(self, *args, **kwargs):
self.args = args
self.kwargs = kwargs
return self
def load_module(self, fullname):
# XXX ignoring sys.modules details, e.g. if module already existed.
lazy_module = LazyModule(fullname, proxy=self, name=fullname)
sys.modules[fullname] = lazy_module
return lazy_module
class LazyModule(types.ModuleType):
def __init__(*args, proxy, name, **kwargs):
self.__proxy = proxy
self.__name = name
super().__init__(*args, **kwargs)
def __getattribute__(self, attr):
self.__class__ = Module
state = self.__dict__.copy()
loader = self.__proxy.loader(*self.proxy.args, **self.proxy.kwargs)
# XXX ignoring sys.modules details, e.g. removing module on load
failure.
loader.load_module(self.__name)
self.__dict__.update(state)
return getattr(module, attr)
----------
dependencies: +Make isinstance() work with super type instances
_______________________________________
Python tracker <[email protected]>
<http://bugs.python.org/issue17621>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com