2

this is my code:

class A(object):
   def test(self): pass
class B(A): pass

my question is when I run super(B).test, I get the following exception:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'super' object has no attribute 'test'

I saw the python document: "super(type) -> unbound super object", why it didn't work? I hope someone could give an example of using "super(type)" corretly, thanks

Jay
  • 113
  • 8

2 Answers2

2

If you want to invoke test, you do not need to use super here.

b = B()
b.test()

The keyword super in python is usually used in inner class. Thanks for @PM 2Ring's tip.

zebo zhuang
  • 566
  • 1
  • 5
  • 15
  • Thank you for your answer. My question is how to use "super(type)", could you give me an example? thanks – Jay Jun 13 '17 at 01:45
1

This is really odd thing to do, and as zebo said, there's no need to use super here, calling test on an instance of B will call the test method inherited from A. Demo:

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

class B(A): pass

b = B()
b.test()

output

In test <__main__.B object at 0xb715fb6c>

However, it is possible to use super, if you pass it an instance of B:

super(B, b).test()

or

super(B, B()).test()

Both of these lines give the same output as the previous code. And this all works in both Python 2 & 3. (Of course you need to do from __future__ import print_function in Python 2 to access the print function).

PM 2Ring
  • 54,345
  • 6
  • 82
  • 182
  • Why does it only work with instances though? None of `super(A).test`, `super(B).test` and `super(A, B).test` work. EDIT: Turns out `super(B, B).test` works. I'll try to wrap my head around this one... – Aran-Fey Jun 12 '17 at 08:01
  • @Rawing. Weird. Neither `super(A, B).test()` nor `super(B, B).test()` work for me on Python 2.6.6 or 3.6.0. The former raises `AttributeError: 'super' object has no attribute 'test'`, the latter raises `TypeError: unbound method test() must be called with B instance as first argument (got nothing instead)` in Python 2 and `TypeError: test() missing 1 required positional argument: 'self'` in Python 3. – PM 2Ring Jun 12 '17 at 08:24
  • @Rawing The 2 arg form of `super` is either `super(type, obj)`, where `obj` is an instance (derived from) `type`, or `super(type, type2)`. But really they should only be used inside a method. In Python 3, deep magic is used to allow `super` to do its stuff. Take a look at the excellent [Why is Python 3.x's super() magic?](https://stackoverflow.com/q/19608134/4014959) by Zero Piraeus & Martijn Pieters. – PM 2Ring Jun 12 '17 at 08:33
  • Of course you can't call it without an instance, but `super(B, B).test` lets you at least access the `test` method of `A`. I expected `super(B)` to work like that, but it doesn't, so now I'm confused what the point of unbound super objects is... – Aran-Fey Jun 12 '17 at 08:36
  • 1
    @Rawing More wisdom from the ninja: [How can I use super() with one argument in python](https://stackoverflow.com/a/30190341/4014959) – PM 2Ring Jun 12 '17 at 08:44
  • Thank you for your answer. I want to know how to use super() with one argument, i.e. super(type), could you show me an example? – Jay Jun 13 '17 at 01:52
  • @Jay I think Martijn has covered that topic well in the last answer I linked to. And in Python 3, the most common use of `super` is to call it with no arguments. – PM 2Ring Jun 13 '17 at 02:20