ManagerMixin¶
- class mmengine.utils.ManagerMixin(name='', **kwargs)[源代码]¶
ManagerMixin
is the base class for classes that have global access requirements.The subclasses inheriting from
ManagerMixin
can get their global instances.示例
>>> class GlobalAccessible(ManagerMixin): >>> def __init__(self, name=''): >>> super().__init__(name) >>> >>> GlobalAccessible.get_instance('name') >>> instance_1 = GlobalAccessible.get_instance('name') >>> instance_2 = GlobalAccessible.get_instance('name') >>> assert id(instance_1) == id(instance_2)
- 参数:
name (str) – Name of the instance. Defaults to ‘’.
- classmethod check_instance_created(name)[源代码]¶
Check whether the name corresponding instance exists.
- classmethod get_current_instance()[源代码]¶
Get latest created instance.
Before calling
get_current_instance
, The subclass must have calledget_instance(xxx)
at least once.- Examples
>>> instance = GlobalAccessible.get_current_instance() AssertionError: At least one of name and current needs to be set >>> instance = GlobalAccessible.get_instance('name1') >>> instance.instance_name name1 >>> instance = GlobalAccessible.get_current_instance() >>> instance.instance_name name1
- 返回:
Latest created instance.
- 返回类型:
- classmethod get_instance(name, **kwargs)[源代码]¶
Get subclass instance by name if the name exists.
If corresponding name instance has not been created,
get_instance
will create an instance, otherwiseget_instance
will return the corresponding instance.- Examples
>>> instance1 = GlobalAccessible.get_instance('name1') >>> # Create name1 instance. >>> instance.instance_name name1 >>> instance2 = GlobalAccessible.get_instance('name1') >>> # Get name1 instance. >>> assert id(instance1) == id(instance2)