[Tutor] Mixing in and Mixing out classes in python
tomazbevec at yahoo.com
Fri Aug 1 02:09:51 CEST 2008
Thanks for your reply Alan,
I am partially asking out of interest, but I also have a potential application.
I'm working on a simulation of cellular growth patterns (basically cell instances interacting stochastically on a lattice). Anyway, there are many different cell "behaviors" that I have to simulate, and cells can potentially gain and lose these "behaviors" over the course of the simulation. It would be too much to put every behavior function in the cell class, so I'm writing each behavior as a mixin and mixing it in (and initializing it) when necessary or when biological function is gained by the cells.
In addition some of the behaviors would be too big (as in lines of code) if they were implemented in one function so splitting functionality up in my mixin class makes it conceptually easier for me. Also the behaviors sometimes need access to the internals of the cell class. After I mixin a class every method that starts with the word 'behavior.*' in that class is called when I update the simulation. All of my mixins inherit from "object" only.
So far my approach is working well, but I don't have a clean way for cells to lose functionality. Hence my question about mixin out. I tried to just delete the 'behavior.*' functions with the del operator (like for an attribute) but it didn't work.
I have however thought of a number of ways to work around this, and per your suggestion I think I'll just try something else, but if you have any idea on how to dynamically mix out I'd be glad to here it.
--- On Thu, 7/31/08, Alan Gauld <alan.gauld at btinternet.com> wrote:
> From: Alan Gauld <alan.gauld at btinternet.com>
> Subject: Re: [Tutor] Mixing in and Mixing out classes in python
> To: tutor at python.org
> Date: Thursday, July 31, 2008, 3:16 PM
> "Tomaz Bevec" <tomazbevec at yahoo.com> wrote
> > I am using the following function to mixin in classes
> > into specific object instances ...
> > Is there an analogous way to "Mixout"
> classes from an instance at
> > runtime?
> I'm sure it is possible but...
> Are you just asking out of interest?
> Or do you have a real world need for this?
> Using multiple inheritence is a non trivial aspect of OOP
> that is fraught with difficulty and potential side effects.
> Adding a
> mixin at
> run time is difficult enough, removing one would bend my
> brain way too
> far I suspect. (Many coding standards insist on limiting MI
> to two
> or even banning it outright as being too bug prone.)
> Dynamic mixins add a whole new complexity (it's
> somewhat akin to
> the FAQ here about dynamically naming variables) all you
> code has to take account of the new method introduced and
> potential side-effects that may not be handled in your
> code. Unless
> your mixin classes are ultra clean and stateless in their
> you run a risk of breaking things in ways that are almost
> to debug.
> I've never come across a need for dynamic mixin
> additions and
> I'm interested in how you think you might use this.
> (CLOS is the only
> other place I've even seen this discussed and it's
> possible there
> with some under the covers work but I've never seen it
> Alan Gauld
> Author of the Learn to Program web site
> Tutor maillist - Tutor at python.org
More information about the Tutor