Python:使用用户输入作为类名的类工厂 [英] Python: Class factory using user input as class names
问题描述
我想动态地将类属性添加到超类.此外,我想动态创建从该超类继承的类,这些子类的名称应取决于用户的输入.
I want to add class atttributes to a superclass dynamically. Furthermore, I want to create classes that inherit from this superclass dynamically, and the name of those subclasses should depend on user input.
有一个超类"Unit",我可以在运行时向其添加属性.这已经有效.
There is a superclass "Unit", to which I can add attributes at runtime. This already works.
def add_attr (cls, name, value):
setattr(cls, name, value)
class Unit(object):
pass
class Archer(Unit):
pass
myArcher = Archer()
add_attr(Unit, 'strength', 5)
print "Strenght ofmyarcher: " + str(myArcher.strength)
Unit.strength = 2
print "Strenght ofmyarcher: " + str(myArcher.strength)
这将导致所需的输出:
myarcher的实力:5
myarcher的优势:2
This leads to the desired output:
Strenght ofmyarcher: 5
Strenght ofmyarcher: 2
但是现在我不想预定义Archer子类,而是让用户决定如何调用此子类.我已经尝试过这样的事情:
But now I don't want to predefine the subclass Archer, but I'd rather let the user decide how to call this subclass. I've tried something like this:
class Meta(type, subclassname):
def __new__(cls, subclassname, bases, dct):
return type.__new__(cls, subclassname, Unit, dct)
factory = Meta()
factory.__new__("Soldier")
但是没有运气.我想我还不太了解新在这里的功能. 因此,我想要的是
but no luck. I guess I haven't really understood what new does here. What I want as a result here is
class Soldier(Unit):
pass
由工厂创建.如果我用"Knight"作为参数来调用工厂,我想创建一个Unit类的Knight子类.
being created by the factory. And if I call the factory with the argument "Knight", I'd like a class Knight, subclass of Unit, to be created.
有什么想法吗?提前非常感谢!
再见
-萨诺
Any ideas? Many thanks in advance!
Bye
-Sano
推荐答案
要从名称创建类,请使用class
语句并分配名称.观察:
To create a class from a name, use the class
statement and assign the name. Observe:
def meta(name):
class cls(Unit):
pass
cls.__name__ = name
return cls
现在我想我应该解释一下自己,依此类推.使用class语句创建类时,它是动态完成的-等效于调用 type()
.
Now I suppose I should explain myself, and so on. When you create a class using the class statement, it is done dynamically-- it is equivalent of calling type()
.
例如,以下两个片段执行相同的操作:
For example, the following two snippets do the same thing:
class X(object): pass
X = type("X", (object,), {})
类的名称-类型的第一个参数-分配给__name__
,这基本上就是结束(仅在默认的__repr__()
实现中使用__name__
本身是唯一的时间).要创建一个具有动态名称的类,实际上您可以像这样调用类型,或者您也可以随后更改类名称.但是,class
语法的存在是有原因的-它很方便,以后添加和更改内容也很容易.例如,如果您想添加方法,
The name of a class-- the first argument to type-- is assigned to __name__
, and that's basically the end of that (the only time __name__
is itself used is probably in the default __repr__()
implementation). To create a class with a dynamic name, you can in fact call type like so, or you can just change the class name afterward. The class
syntax exists for a reason, though-- it's convenient, and it's easy to add to and change things later. If you wanted to add methods, for example, it would be
class X(object):
def foo(self): print "foo"
def foo(self): print "foo"
X = type("X", (object,), {'foo':foo})
,依此类推.因此,我建议您使用class语句-如果您从一开始就知道可以这样做,那么您很可能会这样做.处理type
等等都是一团糟.
and so on. So I would advise using the class statement-- if you had known you could do so from the beginning, you likely would have done so. Dealing with type
and so on is a mess.
(顺便说一句,您不应手动呼叫type.__new__()
,而只能呼叫type()
)
(You should not, by the way, call type.__new__()
by hand, only type()
)
这篇关于Python:使用用户输入作为类名的类工厂的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!