Mixins - why and when? (Ruby vs. Python) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 9

Mixins - why and when? (Ruby vs. Python)

I've been somewhat experimenting with Python for quite a time. Class inheritance is a powerful concept and I favor simple superclass-class-subclass usage as I kind of got used to it. Here comes Ruby! With its weird but at the same time tempting concept of mixins. As I think I understand that mechanics, could anyone (especially the more experienced) explain to me the real advantage of it, that could not be replicated in Python? Is it just simplicity of usage? Any problem-solving examples would be great, too.

18th Dec 2016, 8:18 PM
Kuba SiekierzyƄski
Kuba SiekierzyƄski - avatar
3 Answers
+ 4
So far I got through a basic course on Django and haven't touched the subject deep enough. Does it have more syntax features than "pure" Python? I have to check it out, thanks mate! (y)
19th Dec 2016, 6:16 PM
Kuba SiekierzyƄski
Kuba SiekierzyƄski - avatar
+ 3
Python does have the concept of mixins. Django (a popular web framework) makes heavy use of them. A mixing is simply a class that implements a single feature in a way that allows you to easily add it to a new class. In Django this is done to add things to views like the ability to query the database for a list of items while also adding a second mixin to add support for filtering. Mixins are great for providing code reuse without adding more features to a class than are strictly necessary. The tough part about writing Mixins is making them generic so that you aren't requiring a consumer of your mixin to have to do a lot of work.
19th Dec 2016, 5:56 PM
James Durand
James Durand - avatar
+ 3
In Python a Mixin is just a class like any other so no special syntax is needed.
19th Dec 2016, 6:18 PM
James Durand
James Durand - avatar