can someone explain 'super' to me?

Discussion in 'Python' started by Michael, Dec 5, 2009.

  1. Michael

    Michael Guest

    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
    Michael, Dec 5, 2009
    #1
    1. Advertising

  2. Michael

    Lie Ryan Guest

    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).
    Lie Ryan, Dec 5, 2009
    #2
    1. Advertising

  3. En Sat, 05 Dec 2009 07:27:54 -0300, Michael <>
    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
    Gabriel Genellina, Dec 8, 2009
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ian
    Replies:
    0
    Views:
    1,829
  2. suzy
    Replies:
    3
    Views:
    442
  3. BluDog

    Can Someone Please Explain...

    BluDog, Oct 4, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    476
    BluDog
    Oct 5, 2004
  4. Guest

    super.super.super how?

    Guest, Feb 19, 2005, in forum: Java
    Replies:
    24
    Views:
    10,762
    Darryl Pierce
    Feb 24, 2005
  5. Rubyist
    Replies:
    1
    Views:
    98
    Ross Bamford
    Feb 2, 2006
Loading...

Share This Page