Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use super(type) in python?

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

like image 569
Ella Sharakanski Avatar asked Nov 01 '22 11:11

Ella Sharakanski


1 Answers

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.

like image 58
georg Avatar answered Nov 10 '22 19:11

georg