Python 2.3.3 super() behaviour

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Nicolas Lehuen

    #1

    Python 2.3.3 super() behaviour

    Hi,

    I hope this is not a FAQ, but I have trouble understanding the behaviour of
    the super() built-in function. I've read the excellent book 'Python in a
    Nutshell' which explains this built-in function on pages 89-90. Based on the
    example on page 90, I wrote this test code :

    class A(object):
    def test(self):
    print 'A'

    class B(object):
    def test(self):
    print 'B'

    class C(A,B):
    def test(self):
    super(C,self).t est()
    print 'C'

    print C.__mro__
    c=C()
    c.test()

    The output is :
    (<class '__main__.C'>, <class '__main__.A'>, <class '__main__.B'>, <type
    'object'>)
    A
    C

    Whereas I was expecting :
    (<class '__main__.C'>, <class '__main__.A'>, <class '__main__.B'>, <type
    'object'>)
    A
    B
    C

    Was I wrong to expect this (based on what I've read ?)

    Regards,
    Nicolas




  • Peter Otten

    #2
    Re: Python 2.3.3 super() behaviour

    Nicolas Lehuen wrote:
    [color=blue]
    > Hi,
    >
    > I hope this is not a FAQ, but I have trouble understanding the behaviour
    > of the super() built-in function. I've read the excellent book 'Python in
    > a Nutshell' which explains this built-in function on pages 89-90. Based on
    > the example on page 90, I wrote this test code :
    >
    > class A(object):
    > def test(self):
    > print 'A'
    >
    > class B(object):
    > def test(self):
    > print 'B'
    >
    > class C(A,B):
    > def test(self):
    > super(C,self).t est()
    > print 'C'
    >
    > print C.__mro__
    > c=C()
    > c.test()
    >
    > The output is :
    > (<class '__main__.C'>, <class '__main__.A'>, <class '__main__.B'>, <type
    > 'object'>)
    > A
    > C
    >
    > Whereas I was expecting :
    > (<class '__main__.C'>, <class '__main__.A'>, <class '__main__.B'>, <type
    > 'object'>)
    > A
    > B
    > C
    >
    > Was I wrong to expect this (based on what I've read ?)[/color]

    As soon as a test() method without the super(...).test () is reached, no
    further test methods will be invoked. Only the first in the list of base
    classes will be invoked. If I'm getting it right you have to do something
    like:

    class Base(object):
    def test(self):
    print "base"

    class D1(Base):
    def test(self):
    super(D1, self).test()
    print "derived 1"

    class D2(Base):
    def test(self):
    super(D2, self).test()
    print "derived 2"

    class All(D1, D2):
    pass

    All().test()

    Here all cooperating methods have a super() call, and the base class acts as
    a showstopper to prevent that Python tries to invoke the non-existent
    object.test().

    Peter



    Comment

    • Nicolas Lehuen

      #3
      Re: Python 2.3.3 super() behaviour

      OK, I get it now, thanks.

      super() method calls should only be used for method involved in
      diamond-shaped inheritance. This is logical since in this case the base
      classe (from which the diamond-shaped inheritance starts) defines the
      interface of the method.

      This solves another question I was asking myself about super() : "how can it
      work when the method signature differ between B and C ?". Answer : the
      method signature should not change because polymorphic calls would be
      greatly endangered. The base class defines the signature of the method which
      must be followed by all its children, this way super() can work properly.

      The base method signature is not enforced by Python, of course, but you'd
      better respect it unless you get weird result in polymorphic calls.

      Regards,
      Nicolas

      "Peter Otten" <__peter__@web. de> a écrit dans le message de
      news:c65fbo$1q4 $05$1@news.t-online.com...[color=blue]
      > Nicolas Lehuen wrote:
      >[color=green]
      > > Hi,
      > >
      > > I hope this is not a FAQ, but I have trouble understanding the behaviour
      > > of the super() built-in function. I've read the excellent book 'Python[/color][/color]
      in[color=blue][color=green]
      > > a Nutshell' which explains this built-in function on pages 89-90. Based[/color][/color]
      on[color=blue][color=green]
      > > the example on page 90, I wrote this test code :
      > >
      > > class A(object):
      > > def test(self):
      > > print 'A'
      > >
      > > class B(object):
      > > def test(self):
      > > print 'B'
      > >
      > > class C(A,B):
      > > def test(self):
      > > super(C,self).t est()
      > > print 'C'
      > >
      > > print C.__mro__
      > > c=C()
      > > c.test()
      > >
      > > The output is :
      > > (<class '__main__.C'>, <class '__main__.A'>, <class '__main__.B'>, <type
      > > 'object'>)
      > > A
      > > C
      > >
      > > Whereas I was expecting :
      > > (<class '__main__.C'>, <class '__main__.A'>, <class '__main__.B'>, <type
      > > 'object'>)
      > > A
      > > B
      > > C
      > >
      > > Was I wrong to expect this (based on what I've read ?)[/color]
      >
      > As soon as a test() method without the super(...).test () is reached, no
      > further test methods will be invoked. Only the first in the list of base
      > classes will be invoked. If I'm getting it right you have to do something
      > like:
      >
      > class Base(object):
      > def test(self):
      > print "base"
      >
      > class D1(Base):
      > def test(self):
      > super(D1, self).test()
      > print "derived 1"
      >
      > class D2(Base):
      > def test(self):
      > super(D2, self).test()
      > print "derived 2"
      >
      > class All(D1, D2):
      > pass
      >
      > All().test()
      >
      > Here all cooperating methods have a super() call, and the base class acts[/color]
      as[color=blue]
      > a showstopper to prevent that Python tries to invoke the non-existent
      > object.test().
      >
      > Peter
      >
      >
      >[/color]


      Comment

      • Nicolas Lehuen

        #4
        Re: Python 2.3.3 super() behaviour

        The only problem I have is that I want to build a multiple inheritance
        involving an object which does not cooperate, namely :

        class T(object):
        def __init__(self):
        super(T,self)._ _init__()

        class TL(list,object) :
        def __init__(self)
        super(TL,self). __init__()

        In this case, T.__init__ is not called, because list.__init__ does not use
        super(). The only clean way to proceed is to change the inheritance order :
        TL(T,list). This way, both constructors are called.

        Here is another example which exhibits the behaviour :

        class A(object):
        def __init__(self):
        super(A,self)._ _init__()
        print 'A'

        class B(object):
        def __init__(self):
        print 'B'

        class C(B,A):
        def __init__(self):
        super(C,self)._ _init__()
        print 'C'

        class D(A,B):
        def __init__(self):
        super(D,self)._ _init__()
        print 'D'
        [color=blue][color=green][color=darkred]
        >>> C()[/color][/color][/color]
        B
        C
        <__main__.C object at 0x008F3D70>[color=blue][color=green][color=darkred]
        >>> D()[/color][/color][/color]
        B
        A
        D
        <__main__.D object at 0x008F39F0>

        The problem is that if you go further down the inheritance, the behaviour is
        even more complicated :

        class E(object):
        def __init__(self):
        super(E,self)._ _init__()
        print 'E'

        class F(C,E):
        def __init__(self):
        super(F,self)._ _init__()
        print 'F'

        class G(D,E):
        def __init__(self):
        super(G,self)._ _init__()
        print 'G'
        [color=blue][color=green][color=darkred]
        >>> F()[/color][/color][/color]
        B
        C
        F
        <__main__.F object at 0x008F3D70>[color=blue][color=green][color=darkred]
        >>> G()[/color][/color][/color]
        B
        A
        D
        G
        <__main__.G object at 0x008F3EF0>

        class H(E,C):
        def __init__(self):
        super(H,self)._ _init__()
        print 'H'

        class I(E,D):
        def __init__(self):
        super(I,self)._ _init__()
        print 'I'
        [color=blue][color=green][color=darkred]
        >>> H()[/color][/color][/color]
        B
        C
        E
        H
        <__main__.H object at 0x008F3E30>[color=blue][color=green][color=darkred]
        >>> I()[/color][/color][/color]
        B
        A
        D
        E
        I
        <__main__.I object at 0x008F3FD0>

        So the conclusion is : never do that :). Another more constructive
        conclusion would be : always put the most cooperative classes first in the
        inheritance declaration, provided that it doesn't interfere with your needs.
        A class which has an uncooperative ancestor is less cooperative than a class
        which has only cooperative ancestors.

        Regards,
        Nicolas

        "Nicolas Lehuen" <nicolas.lehuen @thecrmcompany. com> a écrit dans le message
        de news:40864674$0 $24834$afc38c87 @news.easynet.f r...[color=blue]
        > OK, I get it now, thanks.
        >
        > super() method calls should only be used for method involved in
        > diamond-shaped inheritance. This is logical since in this case the base
        > classe (from which the diamond-shaped inheritance starts) defines the
        > interface of the method.
        >
        > This solves another question I was asking myself about super() : "how can[/color]
        it[color=blue]
        > work when the method signature differ between B and C ?". Answer : the
        > method signature should not change because polymorphic calls would be
        > greatly endangered. The base class defines the signature of the method[/color]
        which[color=blue]
        > must be followed by all its children, this way super() can work properly.
        >
        > The base method signature is not enforced by Python, of course, but you'd
        > better respect it unless you get weird result in polymorphic calls.
        >
        > Regards,
        > Nicolas
        >
        > "Peter Otten" <__peter__@web. de> a écrit dans le message de
        > news:c65fbo$1q4 $05$1@news.t-online.com...[color=green]
        > > Nicolas Lehuen wrote:
        > >[color=darkred]
        > > > Hi,
        > > >
        > > > I hope this is not a FAQ, but I have trouble understanding the[/color][/color][/color]
        behaviour[color=blue][color=green][color=darkred]
        > > > of the super() built-in function. I've read the excellent book 'Python[/color][/color]
        > in[color=green][color=darkred]
        > > > a Nutshell' which explains this built-in function on pages 89-90.[/color][/color][/color]
        Based[color=blue]
        > on[color=green][color=darkred]
        > > > the example on page 90, I wrote this test code :
        > > >
        > > > class A(object):
        > > > def test(self):
        > > > print 'A'
        > > >
        > > > class B(object):
        > > > def test(self):
        > > > print 'B'
        > > >
        > > > class C(A,B):
        > > > def test(self):
        > > > super(C,self).t est()
        > > > print 'C'
        > > >
        > > > print C.__mro__
        > > > c=C()
        > > > c.test()
        > > >
        > > > The output is :
        > > > (<class '__main__.C'>, <class '__main__.A'>, <class '__main__.B'>,[/color][/color][/color]
        <type[color=blue][color=green][color=darkred]
        > > > 'object'>)
        > > > A
        > > > C
        > > >
        > > > Whereas I was expecting :
        > > > (<class '__main__.C'>, <class '__main__.A'>, <class '__main__.B'>,[/color][/color][/color]
        <type[color=blue][color=green][color=darkred]
        > > > 'object'>)
        > > > A
        > > > B
        > > > C
        > > >
        > > > Was I wrong to expect this (based on what I've read ?)[/color]
        > >
        > > As soon as a test() method without the super(...).test () is reached, no
        > > further test methods will be invoked. Only the first in the list of base
        > > classes will be invoked. If I'm getting it right you have to do[/color][/color]
        something[color=blue][color=green]
        > > like:
        > >
        > > class Base(object):
        > > def test(self):
        > > print "base"
        > >
        > > class D1(Base):
        > > def test(self):
        > > super(D1, self).test()
        > > print "derived 1"
        > >
        > > class D2(Base):
        > > def test(self):
        > > super(D2, self).test()
        > > print "derived 2"
        > >
        > > class All(D1, D2):
        > > pass
        > >
        > > All().test()
        > >
        > > Here all cooperating methods have a super() call, and the base class[/color][/color]
        acts[color=blue]
        > as[color=green]
        > > a showstopper to prevent that Python tries to invoke the non-existent
        > > object.test().
        > >
        > > Peter
        > >
        > >
        > >[/color]
        >
        >[/color]


        Comment

        • David Fraser

          #5
          Re: Python 2.3.3 super() behaviour

          super is not going to be helpful here, so why not call the X.__init__
          functions explicitly?
          Since you *need* multiple __init__ calls from the multiply-inherited
          class, super isn't going to work...

          Nicolas Lehuen wrote:[color=blue]
          > The only problem I have is that I want to build a multiple inheritance
          > involving an object which does not cooperate, namely :
          >
          > class T(object):
          > def __init__(self):
          > super(T,self)._ _init__()
          >
          > class TL(list,object) :
          > def __init__(self)
          > super(TL,self). __init__()
          >
          > In this case, T.__init__ is not called, because list.__init__ does not use
          > super(). The only clean way to proceed is to change the inheritance order :
          > TL(T,list). This way, both constructors are called.
          >
          > Here is another example which exhibits the behaviour :
          >
          > class A(object):
          > def __init__(self):
          > super(A,self)._ _init__()
          > print 'A'
          >
          > class B(object):
          > def __init__(self):
          > print 'B'
          >
          > class C(B,A):
          > def __init__(self):
          > super(C,self)._ _init__()
          > print 'C'
          >
          > class D(A,B):
          > def __init__(self):
          > super(D,self)._ _init__()
          > print 'D'
          >
          >[color=green][color=darkred]
          >>>>C()[/color][/color]
          >
          > B
          > C
          > <__main__.C object at 0x008F3D70>
          >[color=green][color=darkred]
          >>>>D()[/color][/color]
          >
          > B
          > A
          > D
          > <__main__.D object at 0x008F39F0>
          >
          > The problem is that if you go further down the inheritance, the behaviour is
          > even more complicated :
          >
          > class E(object):
          > def __init__(self):
          > super(E,self)._ _init__()
          > print 'E'
          >
          > class F(C,E):
          > def __init__(self):
          > super(F,self)._ _init__()
          > print 'F'
          >
          > class G(D,E):
          > def __init__(self):
          > super(G,self)._ _init__()
          > print 'G'
          >
          >[color=green][color=darkred]
          >>>>F()[/color][/color]
          >
          > B
          > C
          > F
          > <__main__.F object at 0x008F3D70>
          >[color=green][color=darkred]
          >>>>G()[/color][/color]
          >
          > B
          > A
          > D
          > G
          > <__main__.G object at 0x008F3EF0>
          >
          > class H(E,C):
          > def __init__(self):
          > super(H,self)._ _init__()
          > print 'H'
          >
          > class I(E,D):
          > def __init__(self):
          > super(I,self)._ _init__()
          > print 'I'
          >
          >[color=green][color=darkred]
          >>>>H()[/color][/color]
          >
          > B
          > C
          > E
          > H
          > <__main__.H object at 0x008F3E30>
          >[color=green][color=darkred]
          >>>>I()[/color][/color]
          >
          > B
          > A
          > D
          > E
          > I
          > <__main__.I object at 0x008F3FD0>
          >
          > So the conclusion is : never do that :). Another more constructive
          > conclusion would be : always put the most cooperative classes first in the
          > inheritance declaration, provided that it doesn't interfere with your needs.
          > A class which has an uncooperative ancestor is less cooperative than a class
          > which has only cooperative ancestors.
          >
          > Regards,
          > Nicolas
          >
          > "Nicolas Lehuen" <nicolas.lehuen @thecrmcompany. com> a écrit dans le message
          > de news:40864674$0 $24834$afc38c87 @news.easynet.f r...
          >[color=green]
          >>OK, I get it now, thanks.
          >>
          >>super() method calls should only be used for method involved in
          >>diamond-shaped inheritance. This is logical since in this case the base
          >>classe (from which the diamond-shaped inheritance starts) defines the
          >>interface of the method.
          >>
          >>This solves another question I was asking myself about super() : "how can[/color]
          >
          > it
          >[color=green]
          >>work when the method signature differ between B and C ?". Answer : the
          >>method signature should not change because polymorphic calls would be
          >>greatly endangered. The base class defines the signature of the method[/color]
          >
          > which
          >[color=green]
          >>must be followed by all its children, this way super() can work properly.
          >>
          >>The base method signature is not enforced by Python, of course, but you'd
          >>better respect it unless you get weird result in polymorphic calls.
          >>
          >>Regards,
          >>Nicolas
          >>
          >>"Peter Otten" <__peter__@web. de> a écrit dans le message de
          >>news:c65fbo$1 q4$05$1@news.t-online.com...
          >>[color=darkred]
          >>>Nicolas Lehuen wrote:
          >>>
          >>>
          >>>>Hi,
          >>>>
          >>>>I hope this is not a FAQ, but I have trouble understanding the[/color][/color]
          >
          > behaviour
          >[color=green][color=darkred]
          >>>>of the super() built-in function. I've read the excellent book 'Python[/color]
          >>
          >>in
          >>[color=darkred]
          >>>>a Nutshell' which explains this built-in function on pages 89-90.[/color][/color]
          >
          > Based
          >[color=green]
          >>on
          >>[color=darkred]
          >>>>the example on page 90, I wrote this test code :
          >>>>
          >>>>class A(object):
          >>>> def test(self):
          >>>> print 'A'
          >>>>
          >>>>class B(object):
          >>>> def test(self):
          >>>> print 'B'
          >>>>
          >>>>class C(A,B):
          >>>> def test(self):
          >>>> super(C,self).t est()
          >>>> print 'C'
          >>>>
          >>>>print C.__mro__
          >>>>c=C()
          >>>>c.test()
          >>>>
          >>>>The output is :
          >>>>(<class '__main__.C'>, <class '__main__.A'>, <class '__main__.B'>,[/color][/color]
          >
          > <type
          >[color=green][color=darkred]
          >>>>'object'> )
          >>>>A
          >>>>C
          >>>>
          >>>>Whereas I was expecting :
          >>>>(<class '__main__.C'>, <class '__main__.A'>, <class '__main__.B'>,[/color][/color]
          >
          > <type
          >[color=green][color=darkred]
          >>>>'object'> )
          >>>>A
          >>>>B
          >>>>C
          >>>>
          >>>>Was I wrong to expect this (based on what I've read ?)
          >>>
          >>>As soon as a test() method without the super(...).test () is reached, no
          >>>further test methods will be invoked. Only the first in the list of base
          >>>classes will be invoked. If I'm getting it right you have to do[/color][/color]
          >
          > something
          >[color=green][color=darkred]
          >>>like:
          >>>
          >>>class Base(object):
          >>> def test(self):
          >>> print "base"
          >>>
          >>>class D1(Base):
          >>> def test(self):
          >>> super(D1, self).test()
          >>> print "derived 1"
          >>>
          >>>class D2(Base):
          >>> def test(self):
          >>> super(D2, self).test()
          >>> print "derived 2"
          >>>
          >>>class All(D1, D2):
          >>> pass
          >>>
          >>>All().test ()
          >>>
          >>>Here all cooperating methods have a super() call, and the base class[/color][/color]
          >
          > acts
          >[color=green]
          >>as
          >>[color=darkred]
          >>>a showstopper to prevent that Python tries to invoke the non-existent
          >>>object.test( ).
          >>>
          >>>Peter
          >>>
          >>>
          >>>[/color]
          >>
          >>[/color]
          >
          >[/color]

          Comment

          • Nicolas Lehuen

            #6
            Re: Python 2.3.3 super() behaviour

            Ah, so there *is* more than one way to do it, then ? ;)

            What the example shows is that super(...).__in it__ does make one call to
            each superclass' __init__, provided that those superclasses play nicely and
            use super() themselves (as Peter wrote). If one of the superclasses does not
            use super(), the 'magical' iteration over parent methods is interrupted,
            hence the need to put those bad guys at the end of the superclasses list in
            the class declaration.

            But you're right, David, the simplest way to get what I want is to
            explicitely call the superclasses' __init__. However, this would mean that
            super() is not as useful as it seems. I'd rather find a way to *always* use
            super() than have special cases for certain inheritance trees. In other
            words, I'd rather have only one way to do it :).

            Regards,
            Nicolas

            "David Fraser" <davidf@sjsoft. com> a écrit dans le message de
            news:c65j4o$3me $1@ctb-nnrp2.saix.net. ..[color=blue]
            > super is not going to be helpful here, so why not call the X.__init__
            > functions explicitly?
            > Since you *need* multiple __init__ calls from the multiply-inherited
            > class, super isn't going to work...
            >
            > Nicolas Lehuen wrote:[color=green]
            > > The only problem I have is that I want to build a multiple inheritance
            > > involving an object which does not cooperate, namely :
            > >
            > > class T(object):
            > > def __init__(self):
            > > super(T,self)._ _init__()
            > >
            > > class TL(list,object) :
            > > def __init__(self)
            > > super(TL,self). __init__()
            > >
            > > In this case, T.__init__ is not called, because list.__init__ does not[/color][/color]
            use[color=blue][color=green]
            > > super(). The only clean way to proceed is to change the inheritance[/color][/color]
            order :[color=blue][color=green]
            > > TL(T,list). This way, both constructors are called.
            > >
            > > Here is another example which exhibits the behaviour :
            > >
            > > class A(object):
            > > def __init__(self):
            > > super(A,self)._ _init__()
            > > print 'A'
            > >
            > > class B(object):
            > > def __init__(self):
            > > print 'B'
            > >
            > > class C(B,A):
            > > def __init__(self):
            > > super(C,self)._ _init__()
            > > print 'C'
            > >
            > > class D(A,B):
            > > def __init__(self):
            > > super(D,self)._ _init__()
            > > print 'D'
            > >
            > >[color=darkred]
            > >>>>C()[/color]
            > >
            > > B
            > > C
            > > <__main__.C object at 0x008F3D70>
            > >[color=darkred]
            > >>>>D()[/color]
            > >
            > > B
            > > A
            > > D
            > > <__main__.D object at 0x008F39F0>
            > >
            > > The problem is that if you go further down the inheritance, the[/color][/color]
            behaviour is[color=blue][color=green]
            > > even more complicated :
            > >
            > > class E(object):
            > > def __init__(self):
            > > super(E,self)._ _init__()
            > > print 'E'
            > >
            > > class F(C,E):
            > > def __init__(self):
            > > super(F,self)._ _init__()
            > > print 'F'
            > >
            > > class G(D,E):
            > > def __init__(self):
            > > super(G,self)._ _init__()
            > > print 'G'
            > >
            > >[color=darkred]
            > >>>>F()[/color]
            > >
            > > B
            > > C
            > > F
            > > <__main__.F object at 0x008F3D70>
            > >[color=darkred]
            > >>>>G()[/color]
            > >
            > > B
            > > A
            > > D
            > > G
            > > <__main__.G object at 0x008F3EF0>
            > >
            > > class H(E,C):
            > > def __init__(self):
            > > super(H,self)._ _init__()
            > > print 'H'
            > >
            > > class I(E,D):
            > > def __init__(self):
            > > super(I,self)._ _init__()
            > > print 'I'
            > >
            > >[color=darkred]
            > >>>>H()[/color]
            > >
            > > B
            > > C
            > > E
            > > H
            > > <__main__.H object at 0x008F3E30>
            > >[color=darkred]
            > >>>>I()[/color]
            > >
            > > B
            > > A
            > > D
            > > E
            > > I
            > > <__main__.I object at 0x008F3FD0>
            > >
            > > So the conclusion is : never do that :). Another more constructive
            > > conclusion would be : always put the most cooperative classes first in[/color][/color]
            the[color=blue][color=green]
            > > inheritance declaration, provided that it doesn't interfere with your[/color][/color]
            needs.[color=blue][color=green]
            > > A class which has an uncooperative ancestor is less cooperative than a[/color][/color]
            class[color=blue][color=green]
            > > which has only cooperative ancestors.
            > >
            > > Regards,
            > > Nicolas
            > >
            > > "Nicolas Lehuen" <nicolas.lehuen @thecrmcompany. com> a écrit dans le[/color][/color]
            message[color=blue][color=green]
            > > de news:40864674$0 $24834$afc38c87 @news.easynet.f r...
            > >[color=darkred]
            > >>OK, I get it now, thanks.
            > >>
            > >>super() method calls should only be used for method involved in
            > >>diamond-shaped inheritance. This is logical since in this case the base
            > >>classe (from which the diamond-shaped inheritance starts) defines the
            > >>interface of the method.
            > >>
            > >>This solves another question I was asking myself about super() : "how[/color][/color][/color]
            can[color=blue][color=green]
            > >
            > > it
            > >[color=darkred]
            > >>work when the method signature differ between B and C ?". Answer : the
            > >>method signature should not change because polymorphic calls would be
            > >>greatly endangered. The base class defines the signature of the method[/color]
            > >
            > > which
            > >[color=darkred]
            > >>must be followed by all its children, this way super() can work[/color][/color][/color]
            properly.[color=blue][color=green][color=darkred]
            > >>
            > >>The base method signature is not enforced by Python, of course, but[/color][/color][/color]
            you'd[color=blue][color=green][color=darkred]
            > >>better respect it unless you get weird result in polymorphic calls.
            > >>
            > >>Regards,
            > >>Nicolas
            > >>
            > >>"Peter Otten" <__peter__@web. de> a écrit dans le message de
            > >>news:c65fbo$1 q4$05$1@news.t-online.com...
            > >>
            > >>>Nicolas Lehuen wrote:
            > >>>
            > >>>
            > >>>>Hi,
            > >>>>
            > >>>>I hope this is not a FAQ, but I have trouble understanding the[/color]
            > >
            > > behaviour
            > >[color=darkred]
            > >>>>of the super() built-in function. I've read the excellent book 'Python
            > >>
            > >>in
            > >>
            > >>>>a Nutshell' which explains this built-in function on pages 89-90.[/color]
            > >
            > > Based
            > >[color=darkred]
            > >>on
            > >>
            > >>>>the example on page 90, I wrote this test code :
            > >>>>
            > >>>>class A(object):
            > >>>> def test(self):
            > >>>> print 'A'
            > >>>>
            > >>>>class B(object):
            > >>>> def test(self):
            > >>>> print 'B'
            > >>>>
            > >>>>class C(A,B):
            > >>>> def test(self):
            > >>>> super(C,self).t est()
            > >>>> print 'C'
            > >>>>
            > >>>>print C.__mro__
            > >>>>c=C()
            > >>>>c.test()
            > >>>>
            > >>>>The output is :
            > >>>>(<class '__main__.C'>, <class '__main__.A'>, <class '__main__.B'>,[/color]
            > >
            > > <type
            > >[color=darkred]
            > >>>>'object'> )
            > >>>>A
            > >>>>C
            > >>>>
            > >>>>Whereas I was expecting :
            > >>>>(<class '__main__.C'>, <class '__main__.A'>, <class '__main__.B'>,[/color]
            > >
            > > <type
            > >[color=darkred]
            > >>>>'object'> )
            > >>>>A
            > >>>>B
            > >>>>C
            > >>>>
            > >>>>Was I wrong to expect this (based on what I've read ?)
            > >>>
            > >>>As soon as a test() method without the super(...).test () is reached, no
            > >>>further test methods will be invoked. Only the first in the list of[/color][/color][/color]
            base[color=blue][color=green][color=darkred]
            > >>>classes will be invoked. If I'm getting it right you have to do[/color]
            > >
            > > something
            > >[color=darkred]
            > >>>like:
            > >>>
            > >>>class Base(object):
            > >>> def test(self):
            > >>> print "base"
            > >>>
            > >>>class D1(Base):
            > >>> def test(self):
            > >>> super(D1, self).test()
            > >>> print "derived 1"
            > >>>
            > >>>class D2(Base):
            > >>> def test(self):
            > >>> super(D2, self).test()
            > >>> print "derived 2"
            > >>>
            > >>>class All(D1, D2):
            > >>> pass
            > >>>
            > >>>All().test ()
            > >>>
            > >>>Here all cooperating methods have a super() call, and the base class[/color]
            > >
            > > acts
            > >[color=darkred]
            > >>as
            > >>
            > >>>a showstopper to prevent that Python tries to invoke the non-existent
            > >>>object.test( ).
            > >>>
            > >>>Peter
            > >>>
            > >>>
            > >>>
            > >>
            > >>[/color]
            > >
            > >[/color][/color]


            Comment

            • Michele Simionato

              #7
              Re: Python 2.3.3 super() behaviour

              "Nicolas Lehuen" <nicolas.lehuen @thecrmcompany. com> wrote in message news:<40863bd7$ 0$25528$afc38c8 7@news.easynet. fr>...[color=blue]
              > Hi,
              >
              > I hope this is not a FAQ, but I have trouble understanding the behaviour of
              > the super() built-in function.[/color]

              Maybe this thread on super will interest you:




              Michele Simionato

              Comment

              • Josef Meile

                #8
                Re: Python 2.3.3 super() behaviour

                Peter Otten wrote:[color=blue]
                > As soon as a test() method without the super(...).test () is reached, no
                > further test methods will be invoked. Only the first in the list of base
                > classes will be invoked. If I'm getting it right you have to do something
                > like:
                >
                > class Base(object):
                > def test(self):
                > print "base"
                >
                > class D1(Base):
                > def test(self):
                > super(D1, self).test()
                > print "derived 1"
                >
                > class D2(Base):
                > def test(self):
                > super(D2, self).test()
                > print "derived 2"
                >
                > class All(D1, D2):
                > pass
                >
                > All().test()[/color]
                Ok, this produces almost what the original poster wanted. You
                just have to invert the order of the base classes in All:
                [color=blue][color=green][color=darkred]
                >>>class All(D2, D1):[/color][/color][/color]
                .... pass
                ....

                then you will get:[color=blue][color=green][color=darkred]
                >>> All().test()[/color][/color][/color]
                base
                derived 1
                derived 2

                However, I don't understand jet why this doesn't print:

                base
                derived 1
                base
                derived 2

                The method test of Base is called just once? Why?
                I taught it was something like:

                All.test() -> D1.test() + D2.test()

                which is the same as:

                (Base.test() + print "derived 1") + (Base.test() + print derived 2")

                Thanks in advanced,
                Josef

                Comment

                • Peter Otten

                  #9
                  Re: Python 2.3.3 super() behaviour

                  Josef Meile wrote:
                  [color=blue]
                  > Peter Otten wrote:[color=green]
                  > > As soon as a test() method without the super(...).test () is reached, no
                  > > further test methods will be invoked. Only the first in the list of
                  > > base classes will be invoked. If I'm getting it right you have to do
                  > > something like:
                  > >
                  > > class Base(object):
                  > > def test(self):
                  > > print "base"
                  > >
                  > > class D1(Base):
                  > > def test(self):
                  > > super(D1, self).test()
                  > > print "derived 1"
                  > >
                  > > class D2(Base):
                  > > def test(self):
                  > > super(D2, self).test()
                  > > print "derived 2"
                  > >
                  > > class All(D1, D2):
                  > > pass
                  > >
                  > > All().test()[/color]
                  > Ok, this produces almost what the original poster wanted. You
                  > just have to invert the order of the base classes in All:
                  >[color=green][color=darkred]
                  > >>>class All(D2, D1):[/color][/color]
                  > ... pass
                  > ...
                  >
                  > then you will get:[color=green][color=darkred]
                  > >>> All().test()[/color][/color]
                  > base
                  > derived 1
                  > derived 2
                  >
                  > However, I don't understand jet why this doesn't print:
                  >
                  > base
                  > derived 1
                  > base
                  > derived 2
                  >
                  > The method test of Base is called just once? Why?
                  > I taught it was something like:
                  >
                  > All.test() -> D1.test() + D2.test()
                  >
                  > which is the same as:
                  >
                  > (Base.test() + print "derived 1") + (Base.test() + print derived 2")
                  >
                  > Thanks in advanced,
                  > Josef[/color]

                  Every instance has just one __dict__, so calling the same Base method twice
                  would at best be redundant. The Python designers are a bit smarter than
                  that and implemented "cooperativ e" methods for newstyle classes. See
                  http://www.python.org/2.2/descrintro.html#cooperation for the details.

                  Peter

                  Comment

                  • A. Lloyd Flanagan

                    #10
                    Re: Python 2.3.3 super() behaviour

                    "Nicolas Lehuen" <nicolas.lehuen @thecrmcompany. com> wrote in message news:<40864ea6$ 0$25528$afc38c8 7@news.easynet. fr>...[color=blue]
                    >
                    > In this case, T.__init__ is not called, because list.__init__ does not use
                    > super(). The only clean way to proceed is to change the inheritance order :
                    > TL(T,list). This way, both constructors are called.
                    >[/color]

                    I'm surprised that a built-in type doesn't work with super(). Was
                    this a design decision, and if so why, or is this something that
                    should be fixed?

                    Comment

                    • David Fraser

                      #11
                      Re: Python 2.3.3 super() behaviour

                      Hi Nicolas

                      Another related idea:
                      Use template functions when you have enough control over the objects:

                      def A(base=object):
                      class A(base):
                      def __init__(self):
                      print "A"
                      super(A, self).__init__( )
                      return A

                      def B(base=object):
                      class B(base):
                      def __init__(self):
                      print "B"
                      super(B, self).__init__( )
                      return B

                      class C(B(A())):
                      def __init__(self):
                      print "C"
                      super(C, self).__init__( )

                      C()

                      This is a bit different to multiple inheritance, but can have nice
                      effects...

                      David

                      Nicolas Lehuen wrote:[color=blue]
                      > Ah, so there *is* more than one way to do it, then ? ;)
                      >
                      > What the example shows is that super(...).__in it__ does make one call to
                      > each superclass' __init__, provided that those superclasses play nicely and
                      > use super() themselves (as Peter wrote). If one of the superclasses does not
                      > use super(), the 'magical' iteration over parent methods is interrupted,
                      > hence the need to put those bad guys at the end of the superclasses list in
                      > the class declaration.
                      >
                      > But you're right, David, the simplest way to get what I want is to
                      > explicitely call the superclasses' __init__. However, this would mean that
                      > super() is not as useful as it seems. I'd rather find a way to *always* use
                      > super() than have special cases for certain inheritance trees. In other
                      > words, I'd rather have only one way to do it :).
                      >
                      > Regards,
                      > Nicolas
                      >
                      > "David Fraser" <davidf@sjsoft. com> a écrit dans le message de
                      > news:c65j4o$3me $1@ctb-nnrp2.saix.net. ..
                      >[color=green]
                      >>super is not going to be helpful here, so why not call the X.__init__
                      >>functions explicitly?
                      >>Since you *need* multiple __init__ calls from the multiply-inherited
                      >>class, super isn't going to work...
                      >>
                      >>Nicolas Lehuen wrote:
                      >>[color=darkred]
                      >>>The only problem I have is that I want to build a multiple inheritance
                      >>>involving an object which does not cooperate, namely :
                      >>>
                      >>>class T(object):
                      >>> def __init__(self):
                      >>> super(T,self)._ _init__()
                      >>>
                      >>>class TL(list,object) :
                      >>> def __init__(self)
                      >>> super(TL,self). __init__()
                      >>>
                      >>>In this case, T.__init__ is not called, because list.__init__ does not[/color][/color]
                      >
                      > use
                      >[color=green][color=darkred]
                      >>>super(). The only clean way to proceed is to change the inheritance[/color][/color]
                      >
                      > order :
                      >[color=green][color=darkred]
                      >>>TL(T,list) . This way, both constructors are called.
                      >>>
                      >>>Here is another example which exhibits the behaviour :
                      >>>
                      >>>class A(object):
                      >>> def __init__(self):
                      >>> super(A,self)._ _init__()
                      >>> print 'A'
                      >>>
                      >>>class B(object):
                      >>> def __init__(self):
                      >>> print 'B'
                      >>>
                      >>>class C(B,A):
                      >>> def __init__(self):
                      >>> super(C,self)._ _init__()
                      >>> print 'C'
                      >>>
                      >>>class D(A,B):
                      >>> def __init__(self):
                      >>> super(D,self)._ _init__()
                      >>> print 'D'
                      >>>
                      >>>
                      >>>
                      >>>>>>C()
                      >>>
                      >>>B
                      >>>C
                      >>><__main__. C object at 0x008F3D70>
                      >>>
                      >>>>>>D()
                      >>>
                      >>>B
                      >>>A
                      >>>D
                      >>><__main__. D object at 0x008F39F0>
                      >>>
                      >>>The problem is that if you go further down the inheritance, the[/color][/color]
                      >
                      > behaviour is
                      >[color=green][color=darkred]
                      >>>even more complicated :
                      >>>
                      >>>class E(object):
                      >>> def __init__(self):
                      >>> super(E,self)._ _init__()
                      >>> print 'E'
                      >>>
                      >>>class F(C,E):
                      >>> def __init__(self):
                      >>> super(F,self)._ _init__()
                      >>> print 'F'
                      >>>
                      >>>class G(D,E):
                      >>> def __init__(self):
                      >>> super(G,self)._ _init__()
                      >>> print 'G'
                      >>>
                      >>>
                      >>>
                      >>>>>>F()
                      >>>
                      >>>B
                      >>>C
                      >>>F
                      >>><__main__. F object at 0x008F3D70>
                      >>>
                      >>>>>>G()
                      >>>
                      >>>B
                      >>>A
                      >>>D
                      >>>G
                      >>><__main__. G object at 0x008F3EF0>
                      >>>
                      >>>class H(E,C):
                      >>> def __init__(self):
                      >>> super(H,self)._ _init__()
                      >>> print 'H'
                      >>>
                      >>>class I(E,D):
                      >>> def __init__(self):
                      >>> super(I,self)._ _init__()
                      >>> print 'I'
                      >>>
                      >>>
                      >>>
                      >>>>>>H()
                      >>>
                      >>>B
                      >>>C
                      >>>E
                      >>>H
                      >>><__main__. H object at 0x008F3E30>
                      >>>
                      >>>>>>I()
                      >>>
                      >>>B
                      >>>A
                      >>>D
                      >>>E
                      >>>I
                      >>><__main__. I object at 0x008F3FD0>
                      >>>
                      >>>So the conclusion is : never do that :). Another more constructive
                      >>>conclusion would be : always put the most cooperative classes first in[/color][/color]
                      >
                      > the
                      >[color=green][color=darkred]
                      >>>inheritanc e declaration, provided that it doesn't interfere with your[/color][/color]
                      >
                      > needs.
                      >[color=green][color=darkred]
                      >>>A class which has an uncooperative ancestor is less cooperative than a[/color][/color]
                      >
                      > class
                      >[color=green][color=darkred]
                      >>>which has only cooperative ancestors.
                      >>>
                      >>>Regards,
                      >>>Nicolas
                      >>>
                      >>>"Nicolas Lehuen" <nicolas.lehuen @thecrmcompany. com> a écrit dans le[/color][/color]
                      >
                      > message
                      >[color=green][color=darkred]
                      >>>de news:40864674$0 $24834$afc38c87 @news.easynet.f r...
                      >>>
                      >>>
                      >>>>OK, I get it now, thanks.
                      >>>>
                      >>>>super() method calls should only be used for method involved in
                      >>>>diamond-shaped inheritance. This is logical since in this case the base
                      >>>>classe (from which the diamond-shaped inheritance starts) defines the
                      >>>>interface of the method.
                      >>>>
                      >>>>This solves another question I was asking myself about super() : "how[/color][/color]
                      >
                      > can
                      >[color=green][color=darkred]
                      >>>it
                      >>>
                      >>>
                      >>>>work when the method signature differ between B and C ?". Answer : the
                      >>>>method signature should not change because polymorphic calls would be
                      >>>>greatly endangered. The base class defines the signature of the method
                      >>>
                      >>>which
                      >>>
                      >>>
                      >>>>must be followed by all its children, this way super() can work[/color][/color]
                      >
                      > properly.
                      >[color=green][color=darkred]
                      >>>>The base method signature is not enforced by Python, of course, but[/color][/color]
                      >
                      > you'd
                      >[color=green][color=darkred]
                      >>>>better respect it unless you get weird result in polymorphic calls.
                      >>>>
                      >>>>Regards,
                      >>>>Nicolas
                      >>>>
                      >>>>"Peter Otten" <__peter__@web. de> a écrit dans le message de
                      >>>>news:c65fbo $1q4$05$1@news. t-online.com...
                      >>>>
                      >>>>
                      >>>>>Nicolas Lehuen wrote:
                      >>>>>
                      >>>>>
                      >>>>>
                      >>>>>>Hi,
                      >>>>>>
                      >>>>>>I hope this is not a FAQ, but I have trouble understanding the
                      >>>
                      >>>behaviour
                      >>>
                      >>>
                      >>>>>>of the super() built-in function. I've read the excellent book 'Python
                      >>>>
                      >>>>in
                      >>>>
                      >>>>
                      >>>>>>a Nutshell' which explains this built-in function on pages 89-90.
                      >>>
                      >>>Based
                      >>>
                      >>>
                      >>>>on
                      >>>>
                      >>>>
                      >>>>>>the example on page 90, I wrote this test code :
                      >>>>>>
                      >>>>>>class A(object):
                      >>>>>> def test(self):
                      >>>>>> print 'A'
                      >>>>>>
                      >>>>>>class B(object):
                      >>>>>> def test(self):
                      >>>>>> print 'B'
                      >>>>>>
                      >>>>>>class C(A,B):
                      >>>>>> def test(self):
                      >>>>>> super(C,self).t est()
                      >>>>>> print 'C'
                      >>>>>>
                      >>>>>>print C.__mro__
                      >>>>>>c=C()
                      >>>>>>c.test( )
                      >>>>>>
                      >>>>>>The output is :
                      >>>>>>(<class '__main__.C'>, <class '__main__.A'>, <class '__main__.B'>,
                      >>>
                      >>><type
                      >>>
                      >>>
                      >>>>>>'object'> )
                      >>>>>>A
                      >>>>>>C
                      >>>>>>
                      >>>>>>Whereas I was expecting :
                      >>>>>>(<class '__main__.C'>, <class '__main__.A'>, <class '__main__.B'>,
                      >>>
                      >>><type
                      >>>
                      >>>
                      >>>>>>'object'> )
                      >>>>>>A
                      >>>>>>B
                      >>>>>>C
                      >>>>>>
                      >>>>>>Was I wrong to expect this (based on what I've read ?)
                      >>>>>
                      >>>>>As soon as a test() method without the super(...).test () is reached, no
                      >>>>>further test methods will be invoked. Only the first in the list of[/color][/color]
                      >
                      > base
                      >[color=green][color=darkred]
                      >>>>>classes will be invoked. If I'm getting it right you have to do
                      >>>
                      >>>something
                      >>>
                      >>>
                      >>>>>like:
                      >>>>>
                      >>>>>class Base(object):
                      >>>>> def test(self):
                      >>>>> print "base"
                      >>>>>
                      >>>>>class D1(Base):
                      >>>>> def test(self):
                      >>>>> super(D1, self).test()
                      >>>>> print "derived 1"
                      >>>>>
                      >>>>>class D2(Base):
                      >>>>> def test(self):
                      >>>>> super(D2, self).test()
                      >>>>> print "derived 2"
                      >>>>>
                      >>>>>class All(D1, D2):
                      >>>>> pass
                      >>>>>
                      >>>>>All().test ()
                      >>>>>
                      >>>>>Here all cooperating methods have a super() call, and the base class
                      >>>
                      >>>acts
                      >>>
                      >>>
                      >>>>as
                      >>>>
                      >>>>
                      >>>>>a showstopper to prevent that Python tries to invoke the non-existent
                      >>>>>object.tes t().
                      >>>>>
                      >>>>>Peter
                      >>>>>
                      >>>>>
                      >>>>>
                      >>>>
                      >>>>
                      >>>[/color][/color]
                      >
                      >[/color]

                      Comment

                      • Nicolas Lehuen

                        #12
                        Re: Python 2.3.3 super() behaviour

                        Duh, so it was a FAQ...

                        What's interesting in your post is the fact that super is in fact a class
                        (not a builtin function), and that you can inherit from it to get the
                        expected behaviour :



                        Maybe this should be the default behaviour ?

                        Regards,
                        Nicolas

                        "Michele Simionato" <michele.simion ato@poste.it> a écrit dans le message de
                        news:95aa1afa.0 404210729.18f6d e22@posting.goo gle.com...[color=blue]
                        > "Nicolas Lehuen" <nicolas.lehuen @thecrmcompany. com> wrote in message[/color]
                        news:<40863bd7$ 0$25528$afc38c8 7@news.easynet. fr>...[color=blue][color=green]
                        > > Hi,
                        > >
                        > > I hope this is not a FAQ, but I have trouble understanding the behaviour[/color][/color]
                        of[color=blue][color=green]
                        > > the super() built-in function.[/color]
                        >
                        > Maybe this thread on super will interest you:
                        >
                        >[/color]
                        http://groups.google.it/groups?hl=it....lang.python.*[color=blue]
                        >
                        >
                        > Michele Simionato[/color]


                        Comment

                        Working...