Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > can someone explain 'super' to me?

Reply
Thread Tools

can someone explain 'super' to me?

 
 
Michael
Guest
Posts: n/a
 
      12-05-2009
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



 
Reply With Quote
 
 
 
 
Lie Ryan
Guest
Posts: n/a
 
      12-05-2009
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).
 
Reply With Quote
 
 
 
 
Gabriel Genellina
Guest
Posts: n/a
 
      12-08-2009
En Sat, 05 Dec 2009 07:27:54 -0300, Michael <(E-Mail Removed)>
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/viewpo...?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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
70-292: can someone please verify / explain the answer to this question Tim Moor MCSE 7 12-18-2005 03:30 AM
Someone can explain this to me? Tosh Cisco 4 11-19-2005 03:48 PM
Can someone explain this route-map command bhamoo@gmail.com Cisco 8 02-04-2005 12:35 PM
Can someone explain this? Ray Microsoft Certification 1 09-01-2003 02:12 AM
Can someone explain MergableProperty to me? Ian ASP .Net 0 06-24-2003 10:37 PM



Advertisments