[SciPy-dev] Generic polynomials class (was Re: Volunteer for Scipy Project)
Charles R Harris
Tue Oct 13 09:00:30 CDT 2009
On Tue, Oct 13, 2009 at 3:12 AM, Pauli Virtanen <firstname.lastname@example.org<pav%2Bsp@iki.fi>
> Mon, 12 Oct 2009 20:33:08 -0600, Charles R Harris wrote:
> > I've just gotten back and have been playing with the cat/dog/pet thing
> > just to test things out and it looks like it is going to work, I've
> > attached copies of the silly things so you can see an example of what
> > I'm thinking of. Now I'll go take a closer look at what you've been
> > doing, which I probably should have done before running on like this ;)
> That's sort of magicky: the user ends with instances of class named
> Polynomial, which are actually not necessarily of the same class.
> How about using class mixins / multiple inheritance instead?
I just don't think inheritance is appropriate here. I'll admit the proposed
implementation has a magicky flavor, but in my case that arises because the
programming idiom using closures is unfamiliar. In C++ what I would have is
a templated class, but python doesn't have such, while it does have
closures. I suspect folks who do more functional programming might find the
approach more familiar. In Python it's really no different than copying the
interface code into the various modules and changing the underlying function
names with an editor. In one case the result is compiled in the module
environment, in the latter in the function environment. The thing is, I can
change the function environment by what gets passed in the arguments.
> class CatMixin:
> def sound(self):
> return "meow"
> class DogMixin:
> def sound(self):
> return "woof"
> class Pet:
> def speak(self):
> return self.sound()
> def breed(self, other):
> if isinstance(other, Pet):
> return self.__class__()
> raise ValueError("Oh no!")
> class Cat(Pet, CatMixin):
> class Dog(Pet, DogMixin):
> dog = Dog()
> cat = Cat()
> print "Cat:", cat.speak()
> print "Dog:", dog.speak()
> mongrel = dog.breed(cat)
> print "Mongrel:", mongrel.speak()
The dog/cat cross didn't raise an error as it should. Dog and Cat really
need to be different classes.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Scipy-dev