Re: [Tutor] What is a mixin class?

2007-01-19 Thread Danny Yoo
> Wow, Danny.  I am impressed.  I am not sure that this should not be 
> called a 'mash-up' rather than a 'mix-in' but it has really been food 
> for thought.

Thank you, but this isn't an original contribution.  I mostly adapted the 
stuff in papers like:

 http://www.cs.utah.edu/plt/publications/aplas06-fff.pdf


> What else have you got stashed away?  Have you got a book on the way?

Not yet.


> You should at least put this recipe into the Python Cookbook. There is 
> not much on mix-ins in there.

The example I gave isn't quite right because of the thread-safety issue. 
I should revisit that observer code later to handle thread safety before 
putting it into the Cookbook.


Best of wishes!
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] What is a mixin class?

2007-01-18 Thread Alan Gauld

"Don Taylor" <[EMAIL PROTECTED]> wrote

>I have a vague idea what a mixin class is, I would like to get a 
>better
> handle on it.

The mixin style of OOP comes from the old (1970's vintage) Flavors
version of Lisp. In Flavors the metaphor was to start with the Vanilla
flavor and mix-in other flavours as you needed.

In Flavors it was common to define lots of small abstract classes
(mixins) and then the application xclass would inherit from as
many mixins as needed. I have seen classes with as many
as 30 or more mixins and absolutely no methods of their own,
they were all inherited.

For this style to work well requires that mixins are type neutral.
Or more specifically they use "Duck Typing", or are protocol based.
That makes languages like Python natural homes for mixins.

Although mixins can inherit from other base classes it is
traditional that they stand alone so that thee is no risk of
double inheritance of a base class and subsequent confusion
in the class dispatch table.

Mixins are particularly uiseful as a way to add common functionality
to groups of classes that are otherwise not related in a type 
heirarchy.
Error or Security logging is a common application where you can
simply create logging versions of non logging classes by defining
the logging version to inherit from the original class and the logging
mixin. Once that is done you can use the new class exactly like
the old one and get logging without having to write any new code.

The downside is that the heavy use of mixins can be confusing
to debug, introduce unexpected side effects, introduce unexpected
dependency loops and spread bugs across lots of classes - making
them hard to pin down...

> I guess that because of multiple inheritance Python does not need a
> formal way of specifying mixin classes so I presume that there is 
> some
> conventional interpretation/coding that is followed for mixin 
> classes.

The original mixins were simply abstract classes in Lisp. Modern
languages wishing to use the concept sometimes add explicit
support for mixin type classes, or more usually just rely on
conventions - such as expecting them to be pure abstract classes.

> So, what constitutes a mixin class and what are the conventional 
> ways to
> denote them in code?

Normally I just mention that its intended as a mixin in the doc 
string.
Mixin programming is really more a style of OO programming
using multiple inheritance rather than anything special about
the classes themselves.

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] What is a mixin class?

2007-01-17 Thread Don Taylor
Don Taylor wrote:
> I have a vague idea what a mixin class is, I would like to get a better 
> handle on it.
> 

Thanks for the information and the links, I have a much better idea 
about mix-ins now.  I also found the following informative:

http://www.linuxjournal.com/article/4540
and
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/498124

Don.

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] What is a mixin class?

2007-01-17 Thread Kent Johnson
(resending to the whole list)

Don Taylor wrote:
> I have a vague idea what a mixin class is, I would like to get a better 
> handle on it.
> 
> It is a term that is used quite often in Python circles, but I can't 
> find a definition.
> 
> I guess that because of multiple inheritance Python does not need a 
> formal way of specifying mixin classes so I presume that there is some 
> conventional interpretation/coding that is followed for mixin classes.
> 
> So, what constitutes a mixin class and what are the conventional ways to 
> denote them in code?
> 
> Don.
> 
> I notice that Ruby has single inheritance plus mixin classes so maybe 
> somebody who knows Ruby could throw some light on in what way a mixin is 
> different from normal superclass.

A mixin is a class that is not intended to stand on its own, rather it
adds some behaviour to the class that it is mixed in to. Some discussion
here:
http://en.wikipedia.org/wiki/Mixin
http://c2.com/cgi/wiki?Mixin

This page
http://c2.com/cgi/wiki?MixinsForPython
points to SocketServer.ForkingMixin and SocketServer.ThreadingMixin as
examples from the Python standard library.

HTH,
Kent


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] What is a mixin class?

2007-01-17 Thread Danny Yoo


On Wed, 17 Jan 2007, Don Taylor wrote:

> I have a vague idea what a mixin class is, I would like to get a better 
> handle on it.

Hi Don,

This post might help:

 http://mail.python.org/pipermail/tutor/2006-October/050448.html


The core idea is that, since classes themselves are first-class objects, 
we can pass classes around to functions.  More importantly, we can make 
classes on-the-fly.

If we have some class C on hand, we can easily build a subclass.  For 
example:


def do_nothing_mixin(C):
 class S(C):
 pass
 return S


Here, do_nothing_mixing takes in a class C and returns a class S, where S 
is a child subclass of C.  Of course, this example is a bit useless. 
*grin*


But here's a slightly more interesting one:

###
def observer_mixin(C):
 class S(C):
 def __init__(self, *args, **kwargs):
 C.__init__(self, *args, **kwargs)
 self.__listeners = []
 def add_listener(self, l):
 self.__listeners.append(l)
 def notify(self, obj):
 for l in self.__listeners:
 l.notified(obj)
 return S
###


This captures most of the "Observer" design pattern (without 
thread-safety, but it wouldn't be hard to add that feature in).

We can now add observer capabilities to an arbitrary class:

###
class Person:
 def __init__(self, name):
 self.name = name
 def notified(self, obj):
 print "Oh, I %s have been notified about %r" % (self.name, obj)

Announcer = observer_mixin(Person)

ann = Announcer("bart")
others = [Person("Lisa"), Person("Marge"), Person("Homer")]
for o in others:
 ann.add_listener(o)

ann.notify("donut")
###

Note here that we have a pre-existing Person class, and we create a new 
kind of Person called an Announcer.  This Announcer is a person, but can 
also act as an observer.


> It is a term that is used quite often in Python circles, but I can't
> find a definition.

One definition (not the only possible one) could be:

 mixin: a function that takes in an input class and produces
 an output class that's guaranteed to be a subclass of the input.

There are other ways to get mixin-like functionality, which is why it's 
necessary to come to terms.



> I notice that Ruby has single inheritance plus mixin classes so maybe 
> somebody who knows Ruby could throw some light on in what way a mixin is 
> different from normal superclass.

'modules' in Ruby are injectable into the definitions of other classes. 
The mechanism, then, is slightly different than what's presented here, but 
the end effect is pretty similar.


If you have more questions, please feel free to ask.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] What is a mixin class?

2007-01-17 Thread Chris Lasher
On 1/17/07, Don Taylor <[EMAIL PROTECTED]> wrote:
> So, what constitutes a mixin class and what are the conventional ways to
> denote them in code?

A mixin is a specific type of superclass, just called a mixin because
of the concept it represents. A common type of mixin would be a class
that defines some sort of modified functionality intended to be given
to multiple, not necessarily related classes.

Say you wanted all your classes to have a similar looking format when
"print"ed. You could define a mixin class that defined a special
__repr__ method. A detailed  example of this is demonstrated in
/Learning Python/ by Lutz & Ascher.

There's no need to explicitly state that they are a mixin class, but
if you felt the need to, you could either put it in a comment or,
probably even better, in the mixin's docstring.

Chris
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] What is a mixin class?

2007-01-17 Thread Don Taylor
I have a vague idea what a mixin class is, I would like to get a better 
handle on it.

It is a term that is used quite often in Python circles, but I can't 
find a definition.

I guess that because of multiple inheritance Python does not need a 
formal way of specifying mixin classes so I presume that there is some 
conventional interpretation/coding that is followed for mixin classes.

So, what constitutes a mixin class and what are the conventional ways to 
denote them in code?

Don.

I notice that Ruby has single inheritance plus mixin classes so maybe 
somebody who knows Ruby could throw some light on in what way a mixin is 
different from normal superclass.

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor