<html><head><style type='text/css'>p { margin: 0; }</style></head><body><div style='font-family: Times New Roman; font-size: 12pt; color: #000000'><br>----- "Alan Ristow" <alan.ristow@gmail.com> wrote:
<br>> 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>> 
<br><br><div>I will try not to get too religious :o)</div><div>Validate your args in the base class.</div><div><br></div><div>class Base:</div><div>    MIN = None</div><div>    MAX=None</div><div>    self.__init__(self, arg1, arg2):</div><div>         # don't use asserts, they are removed in optimized code</div><div>         if self.MIN is None or self.MAX is None:</div><div>                 raise TypeError('Abstract class')</div><div>         if arg1 < self.MIN or arg2 > self.MAX or arg2 < arg1:</div><div>                 raise ValueError('arg1 arg2 not in range [%s, %s]' % (self.MIN, self.MAX))</div><div>         self.a = arg1</div><div>         self.b = arg2</div><div><br></div><div>class ClassA(Base):</div><div>    MIN = 5</div><div>    MAX = 12</div><div><br></div><div>class ClassB(Base):</div><div>    MIN = 8</div><div>    MAX = 25165 </div><div><br></div><div><br></div><div>JM</div></div></body></html>