mixins

Python mixin with abstract methods overrides concrete methods

Python mixin with abstract methods overrides concrete methods Question: Two mixin classes specify requirements as abstract methods. Together, the classes have a full set of concrete methods. However, they fail to combine into a concrete class: no matter which order I use to declare the concrete class, some abstract methods override the concrete ones. Is …

Total answers: 1

Python abstract base classes, difference between a mixin & abstract method

Python abstract base classes, difference between a mixin & abstract method Question: The table below displays various abstract base classes that are prevalent throughout Python. However, I am uncertain about their specific usage in this context. Can you explain the distinction between the ‘Abstract Methods’ column and the ‘Mixin Methods’ column? Are the methods in …

Total answers: 2

Django: a class based view with mixins and dispatch method

Django: a class based view with mixins and dispatch method Question: Normally, I use a dispatch method of a class based view to set some initial variables or add some logic based on user’s permissions. For example, from django.views.generic import FormView from braces.views import LoginRequiredMixin class GenerateReportView(LoginRequiredMixin, FormView): template_name = ‘reporting/reporting_form.html’ form_class = ReportForm def …

Total answers: 5

Dynamically mixin a base class to an instance in Python

Dynamically mixin a base class to an instance in Python Question: Is it possible to add a base class to an object instance (not a class!) at runtime? Something along the lines of how Object#extend works in Ruby: class Gentleman(object): def introduce_self(self): return “Hello, my name is %s” % self.name class Person(object): def __init__(self, name): …

Total answers: 3

Are Mixin class __init__ functions not automatically called?

Are Mixin class __init__ functions not automatically called? Question: I’d like to use a Mixin to always add some init functionality to my child classes which each inherit from different API base classes. Specifically, I’d like to make multiple different child classes that inherit from one of these different API-supplied base classes and the one …

Total answers: 4

Abstract class + mixin + multiple inheritance in python

Abstract class + mixin + multiple inheritance in python Question: So, I think the code probably explains what I’m trying to do better than I can in words, so here goes: import abc class foo(object): __metaclass__ = abc.ABCMeta @abc.abstractmethod def bar(self): pass class bar_for_foo_mixin(object): def bar(self): print “This should satisfy the abstract method requirement” class …

Total answers: 2

What is a mixin and why is it useful?

What is a mixin and why is it useful? Question: In Programming Python, Mark Lutz mentions the term mixin. I am from a C/C++/C# background and I have not heard the term before. What is a mixin? Reading between the lines of this example (which I have linked to because it is quite long), I …

Total answers: 18