如何从基类动态创建派生类
- 2025-02-27 09:07:00
- admin 原创
- 65
问题描述:
例如我有一个如下的基类:
class BaseClass(object):
def __init__(self, classtype):
self._type = classtype
从此类中我派生出其他几个类,例如
class TestClass(BaseClass):
def __init__(self):
super(TestClass, self).__init__('Test')
class SpecialClass(BaseClass):
def __init__(self):
super(TestClass, self).__init__('Special')
有没有一种好的、pythonic 的方式可以通过函数调用来动态地创建这些类,将新类放入我当前的范围,比如:
foo(BaseClass, "My")
a = MyClass()
...
因为会有评论和问题问我为什么需要这个:派生类都具有完全相同的内部结构,不同之处在于构造函数采用许多以前未定义的参数。因此,例如,MyClass
采用关键字,a
而类的构造函数TestClass
采用b
和c
。
inst1 = MyClass(a=4)
inst2 = MyClass(a=5)
inst3 = TestClass(b=False, c = "test")
但他们绝不应该使用类的类型作为输入参数,例如
inst1 = BaseClass(classtype = "My", a=4)
我已使此方法可行,但更喜欢另一种方式,即动态创建类对象。
解决方案 1:
这段代码允许您创建具有动态名称和参数名称的新类。__init__
just 中的参数验证不允许未知参数,如果您需要其他验证(例如类型)或它们是强制性的,只需在那里添加逻辑:
class BaseClass(object):
def __init__(self, classtype):
self._type = classtype
def ClassFactory(name, argnames, BaseClass=BaseClass):
def __init__(self, **kwargs):
for key, value in kwargs.items():
# here, the argnames variable is the one passed to the
# ClassFactory call
if key not in argnames:
raise TypeError("Argument %s not valid for %s"
% (key, self.__class__.__name__))
setattr(self, key, value)
BaseClass.__init__(self, name[:-len("Class")])
newclass = type(name, (BaseClass,),{"__init__": __init__})
return newclass
它的工作原理如下,例如:
>>> SpecialClass = ClassFactory("SpecialClass", "a b c".split())
>>> s = SpecialClass(a=2)
>>> s.a
2
>>> s2 = SpecialClass(d=3)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 8, in __init__
TypeError: Argument d not valid for SpecialClass
我看到你要求在命名范围中插入动态名称——现在,这在 Python 中不被认为是一个好的做法——你要么有在编码时已知的变量名,要么有数据——并且在运行时学习的名称更多的是“数据”而不是“变量”——
因此,您只需将您的类别添加到字典中并从那里使用它们即可:
name = "SpecialClass"
classes = {}
classes[name] = ClassFactory(name, params)
instance = classes[name](...)
如果您的设计确实需要名称进入范围,只需执行相同的操作,但使用调用返回的字典globals()
而不是任意字典:
name = "SpecialClass"
globals()[name] = ClassFactory(name, params)
instance = SpecialClass(...)
(类工厂函数确实可以在调用者的全局范围内动态插入名称 - 但这是更糟糕的做法,并且在 Python 实现中不兼容。实现这一点的方法是通过sys._getframe(1)f_globals
获取调用者的执行框架,并在其属性中的框架全局字典中设置类名)。
更新,tl;dr:这个答案已经很流行了,但它仍然非常具体到问题主体。关于如何
在 Python 中“从基类动态创建派生类”的一般答案
是简单地调用type
传递新类名、包含基类的元组和__dict__
新类的主体 - 如下所示:
>>> new_class = type("NewClassName", (BaseClass,), {"new_method": lambda self: ...})
更新
任何需要这个的人也应该检查dill项目 - 它声称能够像 pickle 对普通对象一样对类进行 pickle 和 unpickle 操作,并且在我的某些测试中已经实现了这一点。
解决方案 2:
type()
是创建类和特定子类的函数,就像问题中的那样:
def set_x(self, value):
self.x = value
# type() takes as argument the new class name, its base
# classes, and its attributes:
SubClass = type('SubClass', (BaseClass,), {'set_x': set_x})
# (More methods can be put in SubClass, including __init__().)
obj = SubClass()
obj.set_x(42)
print obj.x # Prints 42
print isinstance(obj, BaseClass) # True
解决方案 3:
就我而言:
inst3 = globals()["SpecialClass"](b=False, c = "test")
解决方案 4:
要创建具有动态属性值的类,请查看以下代码。注意:这是 Python 编程语言中的代码片段
def create_class(attribute_data, **more_data): # define a function with required attributes
class ClassCreated(optional extensions): # define class with optional inheritance
attribute1 = adattribute_data # set class attributes with function parameter
attribute2 = more_data.get("attribute2")
return ClassCreated # return the created class
# use class
myclass1 = create_class("hello") # *generates a class*
扫码咨询,免费领取项目管理大礼包!