呼び出し可能なクラスを別のクラスの関数として割り当て、所有する関数から所有者クラスを参照したい:
class DoIt(object):
def __call__(self):
print "%s did it" % self.name
class Doer(object):
def __init__(self, name):
self.name = name
doSomething = DoIt()
doer = Doer("Bob")
doer.doSomething()
上記のコードでは、doSomething()
「Bob did it」を出力する関数が必要ですが、このコードでは
AttributeError: 'DoIt' オブジェクトに属性 'name' がありません
self は Doer インスタンスではなく DoIt インスタンスを参照するためです。Doer インスタンスを参照することは可能ですか?