python 动态创建一个类的示例代码:
# 动态创建类 # class_body 里是类里边的函数,可以写在文件里,从文件中读取 class_body = """ def running_function(self): print(self.name) print("running function") def set_name(self,name): self.name = name """ class_dict = {} # 加载类方法到class_dict里 exec(class_body,globals(),class_dict) # 把class_dict中的方法加载到类里边 customer = type("customer",(object,),class_dict) # 实例化类,调用类方法 c = customer() c.set_name("12") c.running_function()
补充:python动态实例化
python动态实例化
前言
最近在查一个服务的问题时,看到有一段代码if .. elif ...
写了近百行,类似
if command == "xxx": obj = commandx() obj.run() # ... elif command == "yyy": obj = commandy() obj.run() # ... elif command == "zzz": obj = commandz() obj.run() # ... # ...
翻了下git记录,最开始其实只有两三个条件判断,后来command越加越多,就这么延续下来了。
代码逻辑其实没什么问题,也很简单明了,就是看起来有点丑,而且我还开了比较高的桌面缩放,导致一屏幕几乎都是这段if ... elif
看来看去越发觉得丑,先写个demo看看能不能跑通代码。
方式1, 字典映射
如果需要判断的条件比较少,用字典做映射还是挺方便的,但如果条件多,看起来还是挺丑的。
from abc import abc, abstractmethod class abstractcommand(abc): @abstractmethod def run(self): pass class commanda(abstractcommand): def run(self): return "this is command a" class commandb(abstractcommand): def run(self): return "this is command b" class commandc(abstractcommand): def run(self): return "this is command c" class commandfactory: @staticmethod def create_command(command_type: str) -> abstractcommand: command_mapping = { "cmda": commanda, "cmdb": commandb, "cmdc": commandc } cls = command_mapping.get(command_type.lower()) if not cls: raise valueerror(f"unknown command type: {command_type}") return cls() if __name__ == "__main__": cmd = commandfactory.create_command("cmda") assert cmd.run() == "this is command a" cmd = commandfactory.create_command("cmdb") assert cmd.run() == "this is command b" cmd = commandfactory.create_command("cmdc") assert cmd.run() == "this is command cd" # should be exception cmd = commandfactory.create_command("cmdd") # should be exception assert cmd.run() == "this is command d"
方式2, __init_subclass__
《流畅的python(第2版)》的最后一章提到了这个__init__subclass__
,根据python官方文档:
当所在类派生子类时此方法就会被调用。cls 将指向新的子类。如果定义为一个普通实例方法,此方法将被隐式地转换为类方法。传给一个新类的关键字参数会被传给上级类的
__init_subclass__
。 为了与其他使用__init_subclass__
的类兼容,应当去掉需要的关键字参数再将其他参数传给基类。
借助这个机制,可以在实现抽象基类时自动注册子类,避免手动维护注册表。
from abc import abcmeta, abstractmethod from threading import lock from collections import userdict class threadsafedict(userdict): """线程安全的字典""" def __init__(self): super().__init__() self._lock = lock() def __setitem__(self, key, item): with self._lock: super().__setitem__(key, item) class command(metaclass=abcmeta): registry = threadsafedict() def __init__(self): pass @abstractmethod def run(self): pass def __init_subclass__(cls, **kwargs): super().__init_subclass__(**kwargs) cls.registry[cls.__name__.lower()] = cls # 自动注册子类 # 子类定义即自动注册 class commanda(command): def run(self): return "this is command a!" class commandb(command): def run(self): return "this is command b!" class commandc(command): def run(self): return "this is command b!" def create_command(command_type: str) -> command: """工厂函数""" cls = command.registry.get(command_type.lower()) if not cls: raise valueerror(f"unknown command type: {command_type}") return cls() if __name__ == "__main__": cmd = create_command("commanda") assert cmd.run() == "this is command a!" cmd = create_command("commandb") assert cmd.run() == "this is command b!" cmd = create_command("commandc") assert cmd.run() == "this is command cc!" # should be exception cmd = create_command("commandd") assert cmd.run() == "this is command b!" # should be exception
乍一看还是挺不错的,但是也有个缺点,那就是如果各个类分散在不同模块中,那么工厂函数所在的模块就要写一堆from xxx import ...
如果module和类命名比较规范,也可以这么动态加载类
import importlib def create_class(module_name, class_name): module = importlib.import_module(module_name) cls = getattr(module, class_name) return cls()
补充
自动注册类看起来炫,但是对代码阅读来说不是很直观。易读还是美观?这是一个问题。
到此这篇关于python 动态创建一个类的文章就介绍到这了,更多相关python 动态创建一个类内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论