By deamon

2010-05-05 09:23:11 8 Comments

I wonder when to use what flavour of Python 3 super().

Help on class super in module builtins:

class super(object)
 |  super() -> same as super(__class__, <first argument>)
 |  super(type) -> unbound super object
 |  super(type, obj) -> bound super object; requires isinstance(obj, type)
 |  super(type, type2) -> bound super object; requires issubclass(type2, type)

Until now I've used super() only without arguments and it worked as expected (by a Java developer).


  • What does "bound" mean in this context?
  • What is the difference between bound and unbound super object?
  • When to use super(type, obj) and when super(type, type2)?
  • Would it be better to name the super class like in Mother.__init__(...)?


@danodonovan 2015-06-03 08:47:44

A quick note, the new usage of super is outlined in PEP3135 New Super which was implemented in python 3.0. Of particular relevance;

super().foo(1, 2)

to replace the old:

super(Foo, self).foo(1, 2)

@Denis Otkidach 2010-05-05 13:28:15

Let's use the following classes for demonstration:

class A(object):
    def m(self):

class B(A): pass

Unbound super object doesn't dispatch attribute access to class, you have to use descriptor protocol:

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

super object bound to instance gives bound methods:

>>> super(B, B()).m
<bound method B.m of <__main__.B object at 0xb765dacc>>
>>> super(B, B()).m()

super object bound to class gives function (unbound methods in terms of Python 2):

>>> super(B, B).m
<function m at 0xb761482c>
>>> super(B, B).m()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: m() takes exactly 1 positional argument (0 given)
>>> super(B, B).m(B())

See Michele Simionato's "Things to Know About Python Super" blog posts series (1, 2, 3) for more information

@gerrit 2016-11-10 12:11:09

The question is specifically about Python3, but Simionato's blog posts series are about Python2, and mention that The advantage is that you avoid to repeat the name of the class in the calling syntax, since that name is hidden in the mangling mechanism of private names.. This is no longer true in Python3, so at least that one advantage is dated.

@smci 2018-09-10 08:53:33

This answers the OP's "what does each do?" but it doesn't answer "when would you use each one?" Also it doesn't actually explain "unbound" vs "bound"; if B is a class name and b = B() is an instance, then B is an unbound object and b is a bound object.

Related Questions

Sponsored Content

39 Answered Questions

[SOLVED] How to get the current time in Python

  • 2009-01-06 04:54:23
  • user46646
  • 3299798 View
  • 2890 Score
  • 39 Answer
  • Tags:   python datetime time

26 Answered Questions

[SOLVED] Does Python have a ternary conditional operator?

9 Answered Questions

[SOLVED] What does 'super' do in Python?

20 Answered Questions

[SOLVED] Are static class variables possible in Python?

62 Answered Questions

[SOLVED] Calling an external command from Python

10 Answered Questions

[SOLVED] Does Python have a string &#39;contains&#39; substring method?

30 Answered Questions

[SOLVED] Finding the index of an item in a list

  • 2008-10-07 01:39:38
  • Eugene M
  • 3881403 View
  • 3182 Score
  • 30 Answer
  • Tags:   python list indexing

20 Answered Questions

[SOLVED] What are metaclasses in Python?

23 Answered Questions

[SOLVED] Is there a way to run Python on Android?

Sponsored Content