Dynamic import (runtime import)

Giúp bạn import module hoặc class dựa theo tên chuỗi, rất mạnh khi viết plugin hoặc hệ thống mở rộng.

import importlib

module = importlib.import_module("math")
print(module.sqrt(16))  # 4.0

Hoặc:

def dynamic_import(path):
    module_path, class_name = path.rsplit(".", 1)
    module = importlib.import_module(module_path)
    return getattr(module, class_name)

 


Phiên bản #1
Được tạo 15 tháng 4 2025 16:35:58 bởi Đỗ Ngọc Tú
Được cập nhật 18 tháng 4 2025 04:46:33 bởi Đỗ Ngọc Tú