Hi all,<br><br>I am relatively new to Python, though not to programming in general, and using Python 2.6. I have a design problem that I cannot quite decide how to handle and I am hoping for some advice.<br><br>I would like to have three classes, ClassA, ClassB, and ClassC, that are essentially the same, the only difference being that each class has a different range of valid values for its properties. Thus, the obvious solution is to create a base class, then subclass from that and include the required error-checking during initialization and when property values change. The base class might look something like this:<br>
<br>class BaseClass(object):<br>    def __init__(self, arg1, arg2):<br>        self.a = arg1<br>        self.b = arg2<br><br>    @property<br>    def a(self):<br>        return self.a<br><br>    @a.setter<br>    def a(self, new_a):<br>
        self.a = new_a<br><br>    @property<br>    def b(self):<br>        return b<br><br>    @b.setter<br>    def b(self, new_b):<br>        self.b = new_b<br><br>And ClassA might look something like this:<br><br>class ClassA(BaseClass):<br>
    def __init__(self, arg1, arg2):<br>        assert arg1 > 0 and arg2 > 100<br>        BaseClass.__init__(self, arg1, arg2)<br><br>    @a.setter<br>    def a(self, new_a):<br>        assert new_a > 0<br>        self.a = new_a<br>
<br>    @b.setter<br>    def b(self, new_b):<br>        assert new_b < 100<br>        self.b = new_b<br><br>However, instead of rewriting my validation code in several different places, I would prefer to write it one time and keep it in one place. I can write a function or method that I call each time I need to do validation, which is the approach I would take in most languages. However, since I am basically writing several small variations on one theme, it seems like in Python this might be an ideal application for decorators and/or metaclasses.<br>
<br>So my question is, what is the most sensible way to write a set of classes such as these in Python? I am not afraid to experiment with decorators or metaclasses -- indeed, I would love to learn more about them -- but in this particular project I do not want to use them just for the sake of learning to use them. If there is a good reason to use them, I am all for it, though.<br>
<br>All advice appreciated,<br><br>Thanks,<br><br>Alan<br>