super() and type()

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chris Mellon

    #1

    super() and type()

    I see super documented, and in use, as below (from the Python documentation)

    class C(B):
    def meth(self, arg):
    super(C, self).meth(arg)

    I'd like to not write C all the time, so is there any problem with writing:

    class C(B):
    def meth(self, arg):
    super(type(self ), self).meth(arg)


    This seems to work in practice but I don't see it used anywhere and
    I'm worried what I might be missing.

    This was especially brought to my attention because pylint flags the
    second usage as invalid, and I'm not sure if it should be considered a
    false positive or not.
  • Carl Banks

    #2
    Re: super() and type()

    Chris Mellon wrote:
    I see super documented, and in use, as below (from the Python documentation)
    >
    class C(B):
    def meth(self, arg):
    super(C, self).meth(arg)
    >
    I'd like to not write C all the time, so is there any problem with writing:
    >
    class C(B):
    def meth(self, arg):
    super(type(self ), self).meth(arg)
    >
    >
    This seems to work in practice but I don't see it used anywhere and
    I'm worried what I might be missing.
    >
    This was especially brought to my attention because pylint flags the
    second usage as invalid, and I'm not sure if it should be considered a
    false positive or not.
    PyLint is right. Try running this:


    class A(object):
    def meth(self,arg):
    print "hello"

    class B(A):
    def meth(self,arg):
    super(type(self ), self).meth(arg)

    class C(B):
    def meth(self,arg):
    super(type(self ), self).meth(arg)

    print C().meth(1)


    The reason this type(self) returns the type of the object (which
    doesn't change), NOT the type of the class it was defined in. That is,
    type(self) returns C even in the function B.meth. Since C's superclass
    is B, B.meth ends up calling B.meth again, and you get infinite
    recursion.

    Unfortunately, short of hackery, you're stuck with having to write out
    super(C,self).


    Carl Banks

    Comment

    • Rob Williscroft

      #3
      Re: super() and type()

      Chris Mellon wrote in
      news:mailman.77 7.1164653627.32 031.python-list@python.org in
      comp.lang.pytho n:
      I see super documented, and in use, as below (from the Python
      documentation)
      >
      class C(B):
      def meth(self, arg):
      super(C, self).meth(arg)
      >
      I'd like to not write C all the time, so is there any problem with
      writing:
      >
      class C(B):
      def meth(self, arg):
      super(type(self ), self).meth(arg)
      >
      >
      This seems to work in practice but I don't see it used anywhere and
      I'm worried what I might be missing.
      Have you considered what happens if somebody writes:

      class D(C):
      def meth(self, arg):
      super( D, self ).meth( arg )
      >
      This was especially brought to my attention because pylint flags the
      second usage as invalid, and I'm not sure if it should be considered a
      false positive or not.
      Nope, the type argument to super tells it where you are in the
      type hierarchy, type(self) is always the top of the hierarchy.

      Rob.
      --

      Comment

      Working...