[Tutor] intefaces in python
Lie Ryan
lie.1296 at gmail.com
Mon Jun 29 08:51:49 CEST 2009
Bob Rea wrote:
> On Sun June 28 2009 1:48 pm, Alan Gauld wrote:
>> advantages to them over defining, say, a mixin
>
> Noob, here, wanted to know what a mixin is
> eh, just getting back into learning python
> googled it, still not sure what it is, but
> wikipedia says the name come from ice cream mixins at
> Steve's Ice Cream Parlor
>
> Ah yes, i was a frequenter there
>
> Will watch out for what it is when I get farther into python
> may have to name them things like jimmies and
> heath_bar_crunch
>
In object-oriented programming languages, a mixin is a class that
provides a certain functionality to be inherited by a subclass, while
not meant for instantiation (the generating of objects of that class).
Inheriting from a mixin is not a form of specialization but is rather a
means of collecting functionality. A class may inherit most or all of
its functionality from one or more mixins through multiple inheritance.
-- http://en.wikipedia.org/wiki/Mixin
In regular inheritance, you inherit from another class to inherit its
interfaces. In mixin inheritance, you inherit for its implementations.
The mixin concept takes it a bit further; the mixin class (parent class)
may not be usable by itself (i.e. instantiating a mixin class may not
make any sense) and must be inherited .
Example:
# Flavours
class Vanilla(object): pass
class Chocolate(object): pass
# Additions
class Nuts(object): pass
class Cookies(object): pass
# Items
class IceCream(): pass
# Usable classes
class ChocolateIceCream(Chocolate, IceCream): pass
class VanillaAndNutsIceCream(Vanilla, Nuts, IceCream): pass
class ChocolateAndCookiesIceCream(Chocolate, Cookies, IceCream): pass
Vanilla(), Chocolate(), Nuts(), and Cookies() are not meant to be
instantiated directly; they are meant to be inherited. These classes are
called mixin classes.
In python's standard lib, an example of mixin class is threading.Thread()
More information about the Tutor
mailing list