-1
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 传递给班级?

4

1 回答 1

2

只需将它传递给构造函数,就像对任何其他 python 类所做的那样。:-)

return klass(arg1)
于 2012-09-11T11:15:01.517 回答