Before you mark it as duplicate, let me state that I understand how super works and I have read these three links:
What does 'super' do in Python?
Understanding Python super() with __init__() methods
http://python-history.blogspot.nl/2010/06/method-resolution-order.html
This is how super
is supposed to work in case of baseclasses
:
class X(object):
def __init__(self):
print "calling init from X"
super(X, self).__init__()
class Y(object):
def abc(self):
print "calling abc from Y"
super(Y, self).abc()
a = X()
# prints "calling init from X" (works because object possibly has an __init__ method)
b = Y()
b.abc()
# prints "calling abc from Y" and then
# throws error "'super' object has no attribute 'abc'" (understandable because object doesn't have any method named abc)
Question: In django
core implementation, there are several places where they call methods
using super
on classes inheriting from object
(case Y
in my example above). For example: can someone explain me why this code works?
from django.core.exceptions import PermissionDenied
class LoginRequiredMixin(object):
def dispatch(self, request, *args, **kwargs):
if not request.user.is_authenticated():
raise PermissionDenied
return super(LoginRequiredMixin, self).\
dispatch(request, *args, **kwards) # why does this work?
Ref: copied this code from this talk: https://youtu.be/rMn2wC0PuXw?t=403