I think you might use a tuple instead of a list for OrderElement, that would make much easier your code: <br><br>class OrderElement(tuple):                                                                                                                                                                                                                                                     <br>
    def __new__(cls, x, y):<br>        if not isinstance(x, int) or not isinstance(y, int):<br>            raise TypeError("Order element must receives two integers")                                                                                                                                    <br>
        return tuple.__new__(cls, (x, y))<br><br><br>class Order(list):<br>    def __setitem__(self, item):<br>        assert isinstance(item, OrderElement)<br>        super(Order, self).__setitem__(item)<br><br><br>I didn't check your module condition since it isn't quite clear to me, but you could add a second condition two Order class.<br>
<br><div class="gmail_quote">2011/8/7 Steven D'Aprano <span dir="ltr"><<a href="mailto:steve%2Bcomp.lang.python@pearwood.info">steve+comp.lang.python@pearwood.info</a>></span><br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div class="im">Roy Smith wrote:<br>
<br>
> In article <<a href="mailto:mailman.2010.1312731312.1164.python-list@python.org">mailman.2010.1312731312.1164.python-list@python.org</a>>,<br>
>  John O'Hagan <<a href="mailto:research@johnohagan.com">research@johnohagan.com</a>> wrote:<br>
><br>
>> I'm looking for good ways to ensure that attributes are only writable<br>
>> such that they retain the characteristics the class requires.<br>
><br>
> Sounds like you're trying to do<br>
> <a href="http://en.wikipedia.org/wiki/Design_by_contract" target="_blank">http://en.wikipedia.org/wiki/Design_by_contract</a>.  Which is not a bad<br>
> thing.  But, I think a more pythonic way to implement this would be to<br>
> verify behaviors, not types.<br>
><br>
> I would start by writing a assert_invarient() method which validates the<br>
> object.  I'm guessing all you really need is that you can index [0] and<br>
> [1] and get ints, so test for that.  Something like:<br>
><br>
> def assert_invarient(self):<br>
>    try:<br>
>       assert isinstance(data[0], int)<br>
>       assert isinstance(data[1], int)<br>
>    except:<br>
>       raise ValueError<br>
<br>
</div>Don't do that. assert is for testing program logic, not verifying data. The<br>
problem with assert is that the user can turn all assertions off, simply by<br>
launching Python with the -O switch. Your verification code then becomes:<br>
<div class="im"><br>
def assert_invarient(self):<br>
    try:<br>
</div>        pass<br>
    except:<br>
        raise ValueError<br>
<br>
which is useless.<br>
<br>
When should you use an assertion? If you've ever written code like this:<br>
<br>
if condition:<br>
    do_something()<br>
else:<br>
    # This should never happen. But you know what they say: code that<br>
    # can't happen, does!<br>
    raise RuntimeError('condition unexpectedly false')<br>
<br>
<br>
that's a prime candidate for turning into an assertion:<br>
<br>
<br>
assert condition, 'condition unexpectedly false'<br>
do_something()<br>
<br>
<br>
<br>
--<br>
<font color="#888888">Steven<br>
</font><div><div></div><div class="h5"><br>
--<br>
<a href="http://mail.python.org/mailman/listinfo/python-list" target="_blank">http://mail.python.org/mailman/listinfo/python-list</a><br>
</div></div></blockquote></div><br>