Python django中的抽象类和Mixins有什么区别

时间:2022-09-25 12:14:48

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.

任何人都可以告诉我们在Django中抽象类和Mixin有什么区别。我的意思是,如果我们要从基类继承一些方法,为什么有像mixin这样的单独术语,如果它只是一个类。

What is diff between baseclass and mixins

什么是baseclass和mixins之间的差异

1 个解决方案

#1


5  

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.

在Python(和Django)中,mixin是一种多重继承。我倾向于将它们视为“specilist”类,它们为继承它的类(以及其他类)添加了特定的功能。它们并不是真正意义上的独立。

Example with Django's SingleObjectMixin,

Django的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 will enable you to look up the author with just self.get_objects().

添加的SingleObjectMixin将使您只需使用self.get_objects()查找作者。


An abstract class in Python looks like this:

Python中的抽象类看起来像这样:

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. This is mainly because Python utlizes duck typing which kind of removes the need for interfaces. Abstract class enables polymorphism just like interfaces do.

在像Java这样的语言中,有一个接口契约是一个接口。然而,Python没有这样的,你可以得到的最接近的东西是一个抽象类(你也可以阅读abc。这主要是因为Python使用了鸭子类型,这种类型消除了对接口的需求。抽象类使得多态性就像接口做。

#1


5  

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.

在Python(和Django)中,mixin是一种多重继承。我倾向于将它们视为“specilist”类,它们为继承它的类(以及其他类)添加了特定的功能。它们并不是真正意义上的独立。

Example with Django's SingleObjectMixin,

Django的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 will enable you to look up the author with just self.get_objects().

添加的SingleObjectMixin将使您只需使用self.get_objects()查找作者。


An abstract class in Python looks like this:

Python中的抽象类看起来像这样:

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. This is mainly because Python utlizes duck typing which kind of removes the need for interfaces. Abstract class enables polymorphism just like interfaces do.

在像Java这样的语言中,有一个接口契约是一个接口。然而,Python没有这样的,你可以得到的最接近的东西是一个抽象类(你也可以阅读abc。这主要是因为Python使用了鸭子类型,这种类型消除了对接口的需求。抽象类使得多态性就像接口做。