can someone explain 'super' to me?

M

Michael

From the docs about the built-in function super:

----------------------------
super( type[, object-or-type])

Return the superclass of type. If the second argument is omitted the
super object returned is unbound. If the second argument is an object,
isinstance(obj, type) must be true. If the second argument is a type,
issubclass(type2, type) must be true. super() only works for new-style
classes.
A typical use for calling a cooperative superclass method is:

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

Note that super is implemented as part of the binding process for
explicit dotted attribute lookups such as "super(C, self).__getitem__
(name)". Accordingly, super is undefined for implicit lookups using
statements or operators such as "super(C, self)[name]". New in version
2.2.
--------------------------------

It seems like it can return either a class or an instance of a class.
Like
super( C, self)
is like casting self as superclass C.
However if you omit the second argument entirely you get a class.

The former is considered a "bound" object. I'm really not clear on the
idea of "binding" in Python.

any pointers appreciated.
-Mike
 
L

Lie Ryan

It seems like it can return either a class or an instance of a class.
Like
super( C, self)
is like casting self as superclass C.
However if you omit the second argument entirely you get a class.

Inside a class C: these are all equivalent:
super().method(arg) # python 3
super(C, self).method(arg)
super(C).method(self, arg)

it is similar to how you can call
class C(object):
def method(self, arg):
pass

inst = C()
# these are equivalent
inst.method(arg)
C.method(inst, arg)


python 2.x restricts the first argument of an unbound method to instance
of the class; python 3.x does not have such restriction. Thus, it is
possible in python 3.x to have:.... pass
........ def anom(self):
.... print(self)
....<__main__.A object at 0x0165C630>

the same thing in python 2 would be an error.
The former is considered a "bound" object. I'm really not clear on the
idea of "binding" in Python.

The first argument of a bound method (the argument self) is
automatically redirected to the instance. Notice when you called a method:
class A(object):
# this declaration have 3 arguments
def foo(self, a, b):
pass

a = A()
# called by only 2 arguments
a.foo(1, 2)

because a.foo binds method A.foo() and `a`; and `a` is passed implicitly
as the first argument to A.foo(a, 1, 2).
 
G

Gabriel Genellina

You won't get anywhere from the docs in this case, unfortunately. Start by
reading these three articles by Michele Simionato:
http://www.artima.com/weblogs/viewpost.jsp?thread=236275
and also the famous "Python super() considered harmful":
http://fuhm.net/super-harmful/
It seems like it can return either a class or an instance of a class.
Like
super( C, self)
is like casting self as superclass C.

Not really - I hope you'll understand what that means after reading the
above articles, feel free to ask again then.
However if you omit the second argument entirely you get a class.

Those "unbound" super objects are rare; you probably won't need them.
They're discussed in M.S. article, though.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top