diff --git a/mmengine/utils/misc.py b/mmengine/utils/misc.py index 948329f603..b989e16c3a 100644 --- a/mmengine/utils/misc.py +++ b/mmengine/utils/misc.py @@ -532,11 +532,12 @@ def get_object_from_string(obj_name: str): return None # get class or attribute from module + obj = module while True: try: - obj_cls = getattr(module, part) + obj = getattr(obj, part) part = next(parts) except StopIteration: - return obj_cls + return obj except AttributeError: return None diff --git a/tests/test_utils/test_misc.py b/tests/test_utils/test_misc.py index 700cde8759..580a3f2d73 100644 --- a/tests/test_utils/test_misc.py +++ b/tests/test_utils/test_misc.py @@ -336,3 +336,5 @@ def test_locate(): assert get_object_from_string('mmengine.model') is model_module assert get_object_from_string( 'mmengine.model.BaseModel') is model_module.BaseModel + assert get_object_from_string('mmengine.model.BaseModel.forward') is \ + model_module.BaseModel.forward