Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which of the 4 ways to call super() in Python 3 to use?

Tags:

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).

Questions:

  • 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__(...)?
like image 932
deamon Avatar asked May 05 '10 09:05

deamon


People also ask

How do you call a super method in Python?

Using Super(): Python super() function provides us the facility to refer to the parent class explicitly. It is basically useful where we have to call superclass functions. It returns the proxy object that allows us to refer parent class by 'super'.

When super () is used in Python?

The super() function is used to give access to methods and properties of a parent or sibling class. The super() function returns an object that represents the parent class.

What does calling the super () method do?

Definition and Usage It is used to call superclass methods, and to access the superclass constructor. The most common use of the super keyword is to eliminate the confusion between superclasses and subclasses that have methods with the same name.

What is super () __ Init__ in Python?

When you initialize a child class in Python, you can call the super(). __init__() method. This initializes the parent class object into the child class. In addition to this, you can add child-specific information to the child object as well.


2 Answers

Let's use the following classes for demonstration:

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

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()
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())
m

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

like image 113
Denis Otkidach Avatar answered Sep 20 '22 10:09

Denis Otkidach


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)
like image 26
danodonovan Avatar answered Sep 22 '22 10:09

danodonovan