Updated blog post on how to use super()

Billy Mays noway at nohow.com
Wed Jun 1 12:46:32 EDT 2011


On 6/1/2011 12:42 PM, Ian Kelly wrote:
> On Wed, Jun 1, 2011 at 7:03 AM, Billy Mays<noway at nohow.com>  wrote:
>> I read this when it was on HN the other day, but I still don't see what is
>> special about super().  It seems (from your post) to just be a stand in for
>> the super class name?  Is there something special I missed?
>
> It's not a stand-in for the super-class name.  It's a stand-in for
> whatever class is next in the Method Resolution Order (MRO), which is
> determined at run-time and can vary depending on what the actual class
> of the object is.  For example, in this inheritance situation:
>
> class A(object):
>      ...
>
> class B(object):
>      ...
>
> class C(A, B):
>      ...
>
> a = A()
> c = C()
>
> The MRO of A is (A, object).
> The MRO of B is (B, object).
> The MRO of C is (C, A, B, object).
>
> Thus, super(A, a) is going to resolve to object, as you might expect.
> But super(A, c) is going to resolve to B, because the next class after
> A in the MRO for C instances is B.
>
> That's a pretty quick and dirty explanation.  If it doesn't make
> sense, I suggest reading the article again.

What it does is clear to me, but why is it interesting or special isn't. 
  This looks like a small feature that would be useful in a handful of 
cases.

--
Bill



More information about the Python-list mailing list