How can I call a subclass method from parent class ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • bonono@gmail.com

    #1

    How can I call a subclass method from parent class ?

    Hi,

    Suppose my class definition is like this :

    class A:
    name = "A"

    @classmethod
    def foo(cls):
    cls.__super.foo ()
    cls.bar()

    @classmethod
    def bar(cls):
    print cls.name

    class B(A):
    name = "B"

    class C(B):
    name = "C"

    What I want is

    C.foo() prints 'ABC'
    B.foo() prints 'BC'
    A.foo() prints 'A'

    But when I call C.foo(), it said

    AttributeError: class C has no attribute '_A__super'

    How should this be coded ?

  • Jason  Lai

    #2
    Re: How can I call a subclass method from parent class ?

    If you use a newstyle class, e.g. class A(object), then you can get the
    superclass with cls.__base__. You could also use super(cls,cls),
    although note that it returns a <super> object that isn't exactly the
    same thing as a class -- but good enough for just accessing attributes.

    Make sure to check that your superclass isn't <object>, otherwise it'll
    complain about <object> not having a foo attribute. __base__ is
    probably easier for this purpose. Also be careful with multiple
    inheritance.

    No such thing as __super though.

    Comment

    • bonono@gmail.com

      #3
      Re: How can I call a subclass method from parent class ?

      thanks, it works. Though I don't quite understand what super(cls,cls)
      returns, and it doesn't work if I do a super(cls,cls). foo(). But
      cls.__base__.fo o() do the trick.

      thankfully, I don't have multiple inheritance.

      Jason Lai wrote:[color=blue]
      > If you use a newstyle class, e.g. class A(object), then you can get the
      > superclass with cls.__base__. You could also use super(cls,cls),
      > although note that it returns a <super> object that isn't exactly the
      > same thing as a class -- but good enough for just accessing attributes.
      >
      > Make sure to check that your superclass isn't <object>, otherwise it'll
      > complain about <object> not having a foo attribute. __base__ is
      > probably easier for this purpose. Also be careful with multiple
      > inheritance.
      >
      > No such thing as __super though.[/color]

      Comment

      • Steve Holden

        #4
        Re: How can I call a subclass method from parent class ?

        bonono@gmail.co m wrote:[color=blue]
        > Jason Lai wrote:
        >[color=green]
        >>If you use a newstyle class, e.g. class A(object), then you can get the
        >>superclass with cls.__base__. You could also use super(cls,cls),
        >>although note that it returns a <super> object that isn't exactly the
        >>same thing as a class -- but good enough for just accessing attributes.
        >>
        >>Make sure to check that your superclass isn't <object>, otherwise it'll
        >>complain about <object> not having a foo attribute. __base__ is
        >>probably easier for this purpose. Also be careful with multiple
        >>inheritance .
        >>
        >>No such thing as __super though.[/color]
        >
        > thanks, it works. Though I don't quite understand what super(cls,cls)
        > returns, and it doesn't work if I do a super(cls,cls). foo(). But
        > cls.__base__.fo o() do the trick.
        >
        > thankfully, I don't have multiple inheritance.
        >
        >[/color]
        In point of fact super() is most useful when you *do* have multiple
        inheritance. The classic example is when classes B and C have a common
        supertype A, and then class D is a subclass of both B and C.

        Under these circumstances (the so-called "diamond-shaped inheritance
        graph") it can be problematic to ensure that each superclass's methods
        are called exactly once when methods are being extended rather than
        overridden: should a D call B's method, which then calls A's, and if so
        how does a D ensure that C's method gets called without it also calling
        A's method.

        Calls to super() are used to effectively place a linearised oredering on
        the superclasses to ansure that the diamond-shaped inheritance pattern
        is correctly handled to give the correct method resolution order.

        regards
        Steve
        --
        Steve Holden +44 150 684 7255 +1 800 494 3119
        Holden Web LLC www.holdenweb.com
        PyCon TX 2006 www.python.org/pycon/

        Comment

        • bonono@gmail.com

          #5
          Re: How can I call a subclass method from parent class ?

          Thanks for the explanation but some how my code fail and since I don't
          need multiple inheritance for the moment, I would settle for the not so
          clean version.

          The documentation of super is not very clear to me too. As seen in my
          code, I am using classmethod which may cause some problem.

          Steve Holden wrote:
          [color=blue]
          > In point of fact super() is most useful when you *do* have multiple
          > inheritance. The classic example is when classes B and C have a common
          > supertype A, and then class D is a subclass of both B and C.
          >
          > Under these circumstances (the so-called "diamond-shaped inheritance
          > graph") it can be problematic to ensure that each superclass's methods
          > are called exactly once when methods are being extended rather than
          > overridden: should a D call B's method, which then calls A's, and if so
          > how does a D ensure that C's method gets called without it also calling
          > A's method.
          >
          > Calls to super() are used to effectively place a linearised oredering on
          > the superclasses to ansure that the diamond-shaped inheritance pattern
          > is correctly handled to give the correct method resolution order.
          >
          > regards
          > Steve
          > --
          > Steve Holden +44 150 684 7255 +1 800 494 3119
          > Holden Web LLC www.holdenweb.com
          > PyCon TX 2006 www.python.org/pycon/[/color]

          Comment

          Working...