class가 있는 module위치를 import 한 후 class_name으로 class를 호출한다.
import importlib module = importlib.import_module(module_name) class = getattr(module, class_name)
main.py
import importlib def call_by_classname(): class_path = "callable.Callable" path = class_path.split(".") class_name = path[-1] module_path = ".".join(path[:-1]) module = importlib.import_module(module_path) call = getattr(module, class_name)().run() call_by_classname()
callable.py
class Callable: def __init__(self): pass def run(self): print("hello")
'language > python' 카테고리의 다른 글
wkhtmltopdf (0) | 2020.12.06 |
---|---|
python pdf 의 모든 것 (0) | 2020.10.17 |
list.sort() 와 sorted(list) 의 차이 (0) | 2020.03.06 |
python 연산자 (0) | 2020.03.05 |
virtualenv 가상환경 (1) | 2019.07.28 |