Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

can someone explain 'super' to me?

3 views
Skip to first unread message

Michael

unread,
Dec 5, 2009, 5:27:54 AM12/5/09
to
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

Lie Ryan

unread,
Dec 5, 2009, 6:10:28 AM12/5/09
to
On 12/5/2009 9:27 PM, Michael wrote:
> 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:
>>> class A(object):
... pass
...
>>> class B(object):
... def anom(self):
... print(self)
...
>>> a = A()
>>> B.anom(a)
<__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).

Gabriel Genellina

unread,
Dec 8, 2009, 1:55:51 PM12/8/09
to pytho...@python.org
En Sat, 05 Dec 2009 07:27:54 -0300, Michael <michae...@yahoo.com>
escribiᅵ:

>> From the docs about the built-in function super:
>
> ----------------------------
> super( type[, object-or-type])
>

> Return the superclass of type. [...]

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.

--
Gabriel Genellina

0 new messages