pythonにおけるsuper用法の研究

4666 ワード

python言語はC++と似たようなクラス継承があり、クラス定義時にpythonで最初のselfがカスタマイズされ、C++のthisポインタのようにオブジェクト自体を指します.
pythonの簡単なクラスの例:
>>> class hello(object):
...         def print_c():
...             print"hello world!"
>>> hello().print_c()
hello world!

もちろん、実際にはクラスの継承が避けられません.子クラスは親を継承します.通常は次のようになります.
>>> class child(hello):
...         def print_c(self):
...                 hello().print_c()
...                 
>>> child().print_c()
hello world!

pythonではsuper()メカニズムも提供されています.例は次のとおりです.
>>> class hello(object):
...         def print_c(self):
...             print"hello world!"
...             
>>> class child(hello):
...         def print_c(self):
...                 super(child,self).print_c()
...                 
>>> child().print_c()
hello world!

一見同じような感じではありませんか?
pythonにsuper()を導入する目的は、同じベースクラスが一度だけ初期化されることを保証することです(注意:
1 super()メカニズムは多重継承を解決するために用いられており,親クラス名を直接呼び出すことには問題はないが,その後の先人の経験によれば,クラス名で呼び出すか,すべてsuper()を用い,混ぜ合わないように用い,人間として仕事をするには専一のものであるO(∩∩)O~
2 super()継承は新式クラスでのみ使用でき、古典クラスではエラーが発生します.新式クラス:継承されたクラスが必要です.継承されていない場合はobject古典クラスを継承します:親がいません.このときsuper()argument 1 must be type,not classobjを呼び出すとエラーが発生します.
はい、もう一つ例を挙げます.
class parent1(object):
    def __init__(self):
        print 'is parent1'
        print 'goes parent1'

class parent2(object):
    def __init__(self):
        print 'is parent2'
        print 'goes parent2'

class child1(parent1):
    def __init__(self):
        print'is child1'
        parent.__init__(self)
        print 'goes child1'

class child2 (parent1) :
    def __init__(self):
        print 'is child2'
        parent.__init__(self)
        print 'goes child2'

class child3(parent2):
    def __init__(self):
        print 'is child3'
        parent2.__init__(self)
        print 'goes child3'

class grandson(child3,child2,child1):
    def __init__(self):
        print 'is grandson'
        child1.__init__(self)
        child2.__init__(self)
        child3.__init__(self)
        print'goes grandson'


if __name__=='__main__':
    grandson()
is grandson
is child1
is parent1
goes parent1
goes child1
is child2
is parent1
goes parent1
goes child2
is child3
is parent2
goes parent2
goes child3
goes grandson

  ,            ? ,  parent1     ,                  ,        super()      :
class parent1(object):
    def __init__(self):
        super(parent1, self).__init__()
        print 'is parent1'
        print 'goes parent1'

class parent2(object):
    def __init__(self):
        super(parent2, self).__init__()
        print 'is parent2'
        print 'goes parent2'

class child1(parent1):
    def __init__(self):
        print'is child1'
        #parent1.__init__(self)
        super(child1, self).__init__()
        print 'goes child1'

class child2 (parent1) :
    def __init__(self):
        print 'is child2'
        #parent1.__init__(self)
        super(child2, self).__init__()
        print 'goes child2'

class child3(parent2):
    def __init__(self):
        print 'is child3'
        #parent2.__init__(self)
        super(child3, self).__init__()
        print 'goes child3'

class grandson(child3,child2,child1):
    def __init__(self):
        print 'is grandson'
        #child1.__init__(self)
        #child2.__init__(self)
        #child3.__init__(self)
        super(grandson, self).__init__()
        
        print'goes grandson'


if __name__=='__main__':
    grandson()


 
  
is grandson
is child3
is child2
is child1
is parent1
goes parent1
goes child1
goes child2
is parent2
goes parent2
goes child3
goes grandson
の結果、共通ベースクラスparent 1は1回のみ実行されることが明らかになった.
grandsonクラスの継承システムを下図に示します.
       object
           |
         /   \
      P1    P2
    /     \      |
  C1   C2  C3

したがって、クラスの継承システムについては、図とみなすことができ、各クラスはノードとみなすことができ、super()メカニズムの実行順序は、図の広さ優先検索アルゴリズムに従ってsuper()の親を検索する.
次のまとめ:
  1. super()は関数ではなくクラス名であり、super(class,self)は実際にsuperクラスの初期化関数を呼び出し、superオブジェクトを生成する.
2 super()メカニズムは新しいクラスを使用する必要があります.そうしないと、エラーが発生します.
3 super()または直接親呼び出しは、1つの形式のみを選択することが望ましい.