本文整理汇总了Python中types.ModuleType.__setattr__方法的典型用法代码示例。如果您正苦于以下问题:Python ModuleType.__setattr__方法的具体用法?Python ModuleType.__setattr__怎么用?Python ModuleType.__setattr__使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类types.ModuleType
的用法示例。
在下文中一共展示了ModuleType.__setattr__方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __strict__
# 需要导入模块: from types import ModuleType [as 别名]
# 或者: from types.ModuleType import __setattr__ [as 别名]
def __strict__(self):
ModuleType.__setattr__(self, "__class__", NormalModule)
if "." in self.__name__:
parent_name, leaf_name = self.__name__.rsplit(".", 1)
parent = sys.modules[parent_name]
setattr(parent, leaf_name, self) # this will __strict__ the parent
reload(self)
示例2: _make_submodule_autoimporter_module
# 需要导入模块: from types import ModuleType [as 别名]
# 或者: from types.ModuleType import __setattr__ [as 别名]
def _make_submodule_autoimporter_module(ipython, module):
"""
Return a module sub-instance that automatically imports submodules.
Implemented as a factory function to close over the real module.
"""
if not hasattr(module, "__path__"): # We only need to wrap packages.
return module
class SubmoduleAutoImporterModule(ModuleType):
@property
def __dict__(self):
return module.__dict__
# Overriding __setattr__ is needed even when __dict__ is overridden.
def __setattr__(self, name, value):
setattr(module, name, value)
def __getattr__(self, name):
try:
value = getattr(module, name)
if isinstance(value, ModuleType):
value = _make_submodule_autoimporter_module(ipython, value)
return value
except AttributeError:
import_target = "{}.{}".format(self.__name__, name)
try:
submodule = importlib.import_module(import_target)
except getattr(builtins, "ModuleNotFoundError",
ImportError): # Py<3.6.
pass
else:
_report(ipython, "import {}".format(import_target))
return _make_submodule_autoimporter_module(
ipython, submodule)
raise # Raise AttributeError without chaining ImportError.
sai_module = SubmoduleAutoImporterModule(module.__name__)
# Apparently, `module?` does not trigger descriptors, so we need to
# set the docstring explicitly (on the instance, not on the class).
# Then then only difference in the output of `module?` becomes the type
# (`SubmoduleAutoImportModule` instead of `module`), which we should keep
# for clarity.
ModuleType.__setattr__(sai_module, "__doc__", module.__doc__)
return sai_module
示例3: __setattr__
# 需要导入模块: from types import ModuleType [as 别名]
# 或者: from types.ModuleType import __setattr__ [as 别名]
def __setattr__(self, name, value):
if hasattr(self, "_env"):
self._env[name] = value
ModuleType.__setattr__(self, name, value)
示例4: __setattr__
# 需要导入模块: from types import ModuleType [as 别名]
# 或者: from types.ModuleType import __setattr__ [as 别名]
def __setattr__(self, name, value):
if hasattr(self, "__readonly__"):
if self.__readonly__ is True:
raise Exception("This wrapper is marked as read only, it cannot be updated.")
ModuleType.__setattr__(self, name, value)
示例5: __setattr__
# 需要导入模块: from types import ModuleType [as 别名]
# 或者: from types.ModuleType import __setattr__ [as 别名]
def __setattr__(self, attr, value):
if attr not in LazyModule.__reserved_attrs__:
_loadModule(self)
return ModuleType.__setattr__(self, attr, value)
示例6: __init__
# 需要导入模块: from types import ModuleType [as 别名]
# 或者: from types.ModuleType import __setattr__ [as 别名]
def __init__(self, name, file, path=None):
ModuleType.__setattr__(self, '__name__', name)
ModuleType.__setattr__(self, '__file__', file)
if path is not None:
ModuleType.__setattr__(self, '__path__', path)
示例7: __setattr__
# 需要导入模块: from types import ModuleType [as 别名]
# 或者: from types.ModuleType import __setattr__ [as 别名]
def __setattr__(self,attr,value):
_loadModule(self)
return ModuleType.__setattr__(self,attr,value)
示例8: __init__
# 需要导入模块: from types import ModuleType [as 别名]
# 或者: from types.ModuleType import __setattr__ [as 别名]
def __init__(self, name):
ModuleType.__setattr__(self,'__name__',name)
示例9: __setattr__
# 需要导入模块: from types import ModuleType [as 别名]
# 或者: from types.ModuleType import __setattr__ [as 别名]
def __setattr__(self, name, value):
#if hasattr(self, "instances"): self.instances[name] = value
ModuleType.__setattr__(self, name, value)