[Tutor] Mixing in and Mixing out classes in python
Alan Gauld
alan.gauld at btinternet.com
Fri Aug 1 12:21:43 CEST 2008
"Tomaz Bevec" <tomazbevec at yahoo.com> 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.
More information about the Tutor
mailing list