継承されたメソッドでは、キーワード引数は何らかの形で特別に処理されますか?スーパークラスのキーワード引数を持つPython 2.7関数:サブクラスからのアクセス方法?
定義されているクラスのキーワード引数を使用してインスタンスメソッドを呼び出すと、すべてうまく行きます。サブクラスから呼び出すと、あまりにも多くのパラメータが渡されたとPythonは苦情を言います。
例を示します。 "単純な"メソッドはキーワードargsを使用せず、継承はうまく動作します(私にとっても:-) "KW"メソッドはキーワードargsを使用し、継承はもう機能しません...少なくとも私は差。私は予想通り
class aClass(object):
def aSimpleMethod(self, show):
print('show: %s' % show)
def aKWMethod(self, **kwargs):
for kw in kwargs:
print('%s: %s' % (kw, kwargs[kw]))
class aSubClass(aClass):
def anotherSimpleMethod(self, show):
self.aSimpleMethod(show)
def anotherKWMethod(self, **kwargs):
self.aKWMethod(kwargs)
aClass().aSimpleMethod('this')
aSubClass().anotherSimpleMethod('that')
aClass().aKWMethod(show='this')
プリントthis
、that
、およびthis
、。
aSubClass().anotherKWMethod(show='that')
スローしかし:
一度self.aKWMethod(**kwargs)
:
TypeError: aKWMethod() takes exactly 1 argument (2 given)
愚かな間違い - マニュアルが時々助けになります!ありがとう! – virtualnobi