[SciPy-dev] Generic polynomials class (was Re: Volunteer for Scipy Project)

Charles R Harris charlesr.harris@gmail....
Tue Oct 13 09:00:30 CDT 2009


On Tue, Oct 13, 2009 at 3:12 AM, Pauli Virtanen <pav+sp@iki.fi<pav%2Bsp@iki.fi>
> wrote:

> Mon, 12 Oct 2009 20:33:08 -0600, Charles R Harris wrote:
> [clip]
> > 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__()
>        else:
>            raise ValueError("Oh no!")
>
> class Cat(Pet, CatMixin):
>    pass
>
> class Dog(Pet, DogMixin):
>    pass
>
> 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.

Chuck
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/scipy-dev/attachments/20091013/32e91abb/attachment-0001.html 


More information about the Scipy-dev mailing list