pythonsupersupertype

When to use super(type) in python?


When can I use super(type)? Not super(type,obj) but super(type) - with one argument.


Solution

  • From my understanding, super(x) returns an "unbound" descriptor, that is, an object that knows how to get data, but has no idea where. If you assign super(x) to a class attribute and then retrieve it, the descriptor machinery cares for proper binding:

    class A(object):
        def foo(self):
            print 'parent'
    
    class B(A):
        def foo(self):
            print 'child'
    
    B.parent = super(B)
    B().foo()
    B().parent.foo()
    

    See http://www.artima.com/weblogs/viewpost.jsp?thread=236278 for details.