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

Pauli Virtanen pav+sp@iki...
Tue Oct 13 04:12:36 CDT 2009


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?

------------
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()
------------

Now, I'm not 100% opposed to magically generating the Mixins themselves 
with class factories, as they are a step removed from what's visible to 
the user.

-- 
Pauli Virtanen



More information about the Scipy-dev mailing list