Dir*_*irk 33
您可以通过调用type内置函数来动态创建类,并传递适当的参数,例如:
CommentForm = type("CommentForm", (Form,), {
'name': forms.CharField(),
...
})
Run Code Online (Sandbox Code Playgroud)
它适用于新式类.我不确定,这是否适用于旧式课程.
Ign*_*ams 12
几乎可以在任何地方定义类.
def newclass(val):
class C(object):
def __str__(self):
return str(val)
return C
MyClass = newclass(5)
m = MyClass()
print str(m)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7265 次 |
| 最近记录: |