更新:私は現在、最新のエラーに困惑していますが、いくつかの進歩を遂げ、例を簡略化しました。super().__init__
メソッドがバインドされていない理由がわかりません。
import types
class VeryImportantSuperClass(object):
def __init__(self, anArg, anotherArg):
self.anArg = anArg
#Extremely clever code here
def createSubclassAttempt1(name):
source = 'def __init__(self, arg):\n' +\
' super(' + name + ', self).__init__(arg, 6.02e23)\n' +\
' self.foo = self.anArg + 3\n'
cls = type(name, (VeryImportantSuperClass,), {})
d = {name: cls}
exec source in d
cls.__init__ = types.MethodType(d['__init__'], cls)
return cls
if __name__ == '__main__':
cls = createSubclassAttempt1('Foo')
cls('foo')
出力:
Traceback (most recent call last):
File "/home/newb/eclipseWorkspace/TestArea/subclassWithType.py", line 27, in <module>
cls('foo')
File "<string>", line 2, in __init__
TypeError: unbound method __init__() must be called with Foo instance as first argument (got str instance instead)
タイプによって作成されたサブクラスからスーパークラスメソッドを呼び出す方法が必要ですが、その方法がわかれば、私はダッシュします。