メタクラスを使用して次の機能を実装しようとしています:
class foo( object ):
def __init__( self ):
self.val = 'foo'
def bar( self ):
print 'hello world'
print self.val
f = foo()
f.bar() #prints 'hello world' followed by foo
def newbar( self ):
super( **?**, self).bar()
print 'another world!'
fooNew = type('fooNew', (foo,), {'bar':newbar})
n = fooNew()
n.bar() # should print everything in f.bar() followed by 'another world!'
モンキー パッチを使用して、独自の関数 newbar を実装できることを理解しています。ただし、微妙な違いがあります。新しい bar 関数では、最初に基本クラスの bar 関数を実行してから、追加機能を実行する必要があります。
これどうやってするの?またはどうすればこれを改善できますか?