def cclass(arg1):
... if True:
... class klass (object):
... print arg1
... def __init__(self,arg1):
... self.arg1=arg1
... def klassfunc(self):
... arg2=self.arg1
... print arg2
... return klass()
此代码自然会产生错误消息:
>>> test=cclass('astring')
astring
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
File "<interactive input>", line 10, in cclass
TypeError: __init__() takes exactly 2 arguments (1 given)
我的问题是:我应该如何将函数arg1传递给类?
答案 0 :(得分:2)
将其传递给构造函数,就像对任何其他python类一样。 : - )
return klass(arg1)