Re: [Tutor] Mixing in and Mixing out classes in python

2008-08-01 Thread Alan Gauld

Tomaz Bevec [EMAIL PROTECTED] wrote


I'm working on a simulation of cellular growth patterns ...and cells
can potentially gain and lose these behaviors over the course
of the simulation.


OK, That might be a valid scenario.
But personally I'd probably implement that as an
attribute of the cell - a list of behaviours and build
a Behaviour class with subclasses. The methods
of which take a Cell class as their argument.

It is then trivial to add remove behaviours.
And simply call them in a loop

for b in self.behavious:
   b.doit(self)

Or maybe have a dictionary of behaviours
if you even want to call specific ones.

eg

def myMeth(self):
 # do sometjing
 try: self.behaviours['behaviourMyMeth'](self)
 except KeyError: pass
 # do the rest


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,


I'd come back to the old OOP adage, inheritance is for Is-A 
relationships.

Is you Cell a behaviour or does it Have-A behaviour?

With mixins that adage tends to get bent since mixins are often
functional in nature but if the function is not intrinsic to the class
I tend to use delegation.


any idea on how to dynamically mix out I'd be glad to here it.


I don't know but I'm sure its possible if you understand the class
internals well enough.

Regards,

Alan G. 



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


Re: [Tutor] Mixing in and Mixing out classes in python

2008-07-31 Thread Alan Gauld

Tomaz Bevec [EMAIL PROTECTED] 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 (albeit 
powerful)
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 
classes

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 generic
code has to take account of the new method introduced and any
potential side-effects that may not be handled in your code. Unless
your mixin classes are ultra clean and stateless in their 
implementation

you run a risk of breaking things in ways that are almost impossible
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 used!)

Curious,


--
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] Mixing in and Mixing out classes in python

2008-07-31 Thread Tomaz Bevec
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.

Thanks,
Tomaz

 


--- On Thu, 7/31/08, Alan Gauld [EMAIL PROTECTED] wrote:

 From: Alan Gauld [EMAIL PROTECTED]
 Subject: Re: [Tutor] Mixing in and Mixing out classes in python
 To: tutor@python.org
 Date: Thursday, July 31, 2008, 3:16 PM
 Tomaz Bevec [EMAIL PROTECTED] 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
 (albeit 
 powerful)
 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 
 classes
 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
 generic
 code has to take account of the new method introduced and
 any
 potential side-effects that may not be handled in your
 code. Unless
 your mixin classes are ultra clean and stateless in their 
 implementation
 you run a risk of breaking things in ways that are almost
 impossible
 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
 used!)
 
 Curious,
 
 
 -- 
 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


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


Re: [Tutor] Mixing in and Mixing out classes in python

2008-07-31 Thread Kent Johnson
On Thu, Jul 31, 2008 at 8:09 PM, Tomaz Bevec [EMAIL PROTECTED] wrote:
 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.

Perhaps you could keep the behaviors in a dictionary? You could
override __getattr__() in the cell class to look up attributes whose
names start with 'behavior' in the dict.

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