Shortcuts

ManagerMixin

class mmengine.utils.ManagerMixin(name='', **kwargs)[source]

ManagerMixin is the base class for classes that have global access requirements.

The subclasses inheriting from ManagerMixin can get their global instances.

Examples

>>> 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)
Parameters:

name (str) – Name of the instance. Defaults to ‘’.

classmethod check_instance_created(name)[source]

Check whether the name corresponding instance exists.

Parameters:

name (str) – Name of instance.

Returns:

Whether the name corresponding instance exists.

Return type:

bool

classmethod get_current_instance()[source]

Get latest created instance.

Before calling get_current_instance, The subclass must have called get_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
Returns:

Latest created instance.

Return type:

object

classmethod get_instance(name, **kwargs)[source]

Get subclass instance by name if the name exists.

If corresponding name instance has not been created, get_instance will create an instance, otherwise get_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)
Parameters:

name (str) – Name of instance. Defaults to ‘’.

Returns:

Corresponding name instance, the latest instance, or root instance.

Return type:

object

property instance_name: str

Get the name of instance.

Returns:

Name of instance.

Return type:

str