What is the difference between Abstract classes and Mixins in Python django
Question: Can anyone please tell what is the difference between Abstract class and Mixin
in Django. I mean if we are to inherit some methods from base class why there
is separate terminology like mixins if that is just a class.

What is diff between baseclass and mixins


Answer: In Python (and Django), **mixin** is a type of multiple inheritance. I tend to
think of them as "specilist" classes that adds a particular functionality to
the class that inheritates it (along with other classes). They aren't really
meant to stand on their own.

Example with Django's `SingleObjectMixin`,

    
    
    # views.py
    from django.http import HttpResponseForbidden, HttpResponseRedirect
    from django.core.urlresolvers import reverse
    from django.views.generic import View
    from django.views.generic.detail import SingleObjectMixin
    from books.models import Author
    
    class RecordInterest(View, SingleObjectMixin):
        """Records the current user's interest in an author."""
        model = Author
    
        def post(self, request, *args, **kwargs):
            if not request.user.is_authenticated():
                return HttpResponseForbidden()
    
            # Look up the author we're interested in.
            self.object = self.get_object()
            # Actually record interest somehow here!
    
            return HttpResponseRedirect(reverse('author-detail', kwargs={'pk': self.object.pk}))
    

The added
[`SingleObjectMixin`](https://docs.djangoproject.com/en/dev/ref/class-based-
views/mixins-single-object/#django.views.generic.detail.SingleObjectMixin)
will enable you to look up the `author` with just
[`self.get_objects()`](https://docs.djangoproject.com/en/dev/ref/class-based-
views/mixins-single-
object/#django.views.generic.detail.SingleObjectMixin.get_object).

* * *

An abstract class in Python looks like this:

    
    
    class Base(object):
        # This is an abstract class
    
        # This is the method child classes need to implement
        def implement_me(self):
            raise NotImplementedError("told you so!")
    

In languages like Java, there is an `Interface` contract which is an
_interface_. However, Python doesn't have such and the _closest_ thing you can
get is an abstract class (you can also read on
[abc](http://docs.python.org/library/abc.html). This is mainly because Python
utlizes [duck typing](http://en.wikipedia.org/wiki/Duck_typing#In_Python)
which kind of removes the need for interfaces. Abstract class enables
polymorphism just like interfaces do.

