Building class names from a list of strings
本问题已经有最佳答案,请猛点这里访问。
我将
1 2 3 4 5 6 7 8 9 10 11 | class Frame(tk.Frame): def __init__(self, parent, **kwargs): objparams, packparams = dispatch_parameters(self, **kwargs) tk.Frame.__init__(self, parent, objparams) self.pack(packparams) class Label(tk.Label): def __init__(self, parent, **kwargs): objparams, packparams = dispatch_parameters(self, **kwargs) tk.Label.__init__(self, parent, objparams) self.pack(packparams) |
为了避免对这两个类重复相同的代码,我想知道如何通过改变"标签"来重用它。我知道上面的"
1 2 3 4 5 6 | for classname in ["Frame","Label"]: class <<classname>>(tk.<<classname>>): def __init__(self, parent, **kwargs): objparams, packparams = dispatch_parameters(self, **kwargs) tk.<<classname>>.__init__(self, parent, objparams) self.pack(packparams) |
对于这种代码重用的情况,是否有一种Python式的方法?或者我应该坚持一个接一个地定义类,即使代码非常相似?
注1:我认为这个问题与另一个问题非常相似,但对于目标C而言
注2:我故意省略了
可以使用
1 2 3 4 5 6 7 8 9 10 11 12 | def class_factory(classname): base_class = getattr(tk, classname) def __init__(self, parent, **kwargs): objparams, packparams = dispatch_parameters(self, **kwargs) base_class.__init__(self, parent, objparams) self.pack(packparams) return type(classname, (base_class, object), {'__init__': __init__}) for classname in ["Frame","Label"]: globals()[classname] = class_factory(classname) |
当给定3个参数时,
这里需要使用
另一种选择是将
1 2 3 4 5 6 7 8 9 10 11 | def class_factory(classname): base_class = getattr(tk, classname) class CustomClass(base_class): def __init__(self, parent, **kwargs): objparams, packparams = dispatch_parameters(self, **kwargs) base_class.__init__(self, parent, objparams) self.pack(packparams) CustomClass.__name__ = classname return CustomClass |
这里我们不需要混合使用