1

In the python docs, one can read the following about super():

If the second argument is omitted, the super object returned is unbound.

They are referring to the second argument in super(class_name, second_argument). What does it mean for an object to be unbound? I've heard of unbound methods, meaning they don't exist on any object, but on a class (please correct me if I'm wrong), but what is an unbound object?

Here is a code example provided in the same docs. It shows a typical call to super() in order to get the overridden method of the superclass.

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

As you can see, they do use the second argument here. Why should we do that? What would happen if we didn't?

EDIT: I tried the following...

class B(object):
    def method(self, arg):
        print(arg)

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

class D(B):
    def method(self, arg):
        super(D).method(arg)

c = C()
d = D()

c.method("hi")
d.method("hi again")

The call to c.method() worked fine, printing "hi", but the second call, to d.method(), crashed:

n132-p95:Desktop sahandzarrinkoub$ python2 super.py
hi
Traceback (most recent call last):
  File "super.py", line 17, in <module>
    d.method("hi again")
  File "super.py", line 11, in method
    super(D).method(arg)
AttributeError: 'super' object has no attribute 'method'
Sahand
  • 7,980
  • 23
  • 69
  • 137

0 Answers0