私は4つの異なるクラスを持っています。メインの基底/親クラス、この親クラスから継承する2つのメインクラス、およびこれらのメインクラスの両方から継承する別のクラスがあります。同じ名前で親クラスと異なる数の引数を持つメソッドがある場合は、TypeErrorを取得します。同じメソッド名でも異なる引数を持つ複数の継承はTypeErrorを作成します
# Example
class Parent(object):
def check(self, arg):
tmp = {
'one': False,
'two': False
}
try:
if 'one' in arg:
tmp['one'] = True
if 'two' in arg:
tmp['two'] = True
except TypeError:
pass
return tmp
class Child(Parent):
def check(self, arg):
return Parent.check(self, arg)['one']
def method(self, arg):
if self.check(arg):
print 'One!'
class ChildTwo(Parent):
def check(self, arg):
return Parent.check(self, arg)['two']
def method(self, arg):
if self.check(arg):
print 'Two!'
class ChildThree(Child, ChildTwo):
def check(self, arg, arg2):
print arg2
return Child.check(self, arg)
def method(self, arg):
if self.check(arg, 'test'):
print 'One!'
ChildTwo.method(self, arg)
test = ChildThree()
test = test.method('one and two')
runfile('untitled6.py', wdir='./Documents')
test
One!
Traceback (most recent call last):
File "< stdin >", line 1, in < module >
File "C:\Users\py\AppData\Local\Continuum\Anaconda2\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 714, in runfile
execfile(filename, namespace)
File "C:\Users\py\AppData\Local\Continuum\Anaconda2\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 74, in execfile
exec(compile(scripttext, filename, 'exec'), glob, loc)
File "untitled6.py", line 49, in
test = test.method('one and two')
File "untitled6.py", line 46, in method
ChildTwo.method(self, arg)
File "untitled6.py", line 34, in method
if self.check(arg):TypeError: check() takes exactly 3 arguments (2 given)
私は 'ChildThree' の 'チェック' メソッドから二番目の引数を削除する場合しかし、正常に動作するようです:
class ChildThree(Child, ChildTwo):
def check(self, arg):
return Child.check(self, arg)
def method(self, arg):
if self.check(arg):
print 'One!'
ChildTwo.method(self, arg)
runfile('untitled6.py', wdir='./Documents')
One!
Two!
私はにかなり新しいですクラス/継承のため、単一の引数で親クラスメソッドを呼び出すにもかかわらず、余分な引数が原因でTypeErrorが発生する理由がわかりません。
ああ、それは理にかなっています!私は(私は3分待つと言うことができます)私は答えを受け入れるよ。これを修正する良い/ Pythonの方法はありますか?または 'ChildTwo.method'の本文で 'selfTho.check(self、arg)'に 'self.check(arg):'を変更するだけですか? – PyNoob
私がお勧めする方法は、継承の状況で、同じ名前と異なる引数の仕様を持つメソッドを避けることです。それは不必要に複雑に聞こえるだけであり、「ピジョンソニック」の大部分はシンプルさです。 – wim