How to define a class that can create instances of computed type/class?

Ian Bicking ianb at colorstudy.com
Fri Dec 13 18:33:17 EST 2002


On Fri, 2002-12-13 at 15:57, Pierre Rouleau wrote:
> Is there a better way to do this?  Is it possible to create the same 
> effect without using a function?

There are many possible ways to do this, and the way you've come up with
is perfectly fine.  One alternative would be to combine the classes, and
then do something like:

class Form:
    def __init__(self, value):
        if self.isType1(value):
            self.parser = self.parserType1
        else:
            self.parser = self.parserType2

Assuming that the "parser" method is the only difference between the two
types of format -- if there's more, of course you can do more
assignments.

You could also use new-style classes with a meta-class or with the
__new__ method.  But I don't think you'll find them functionally
different from the code you have now and it won't be worth the effort.

-- 
Ian Bicking           Colorstudy Web Development
ianb at colorstudy.com   http://www.colorstudy.com
PGP: gpg --keyserver pgp.mit.edu --recv-keys 0x9B9E28B7
4869 N Talman Ave, Chicago, IL 60625 / (773) 275-7241





More information about the Python-list mailing list