Restricted attribute writing

Rafael Durán Castañeda rafadurancastaneda at gmail.com
Sun Aug 7 18:56:04 CEST 2011


The assert on Order should be an if ... raise, like OrderElement, sorry for
the mistake and repost

El 7 de agosto de 2011 18:53, Rafael Durán Castañeda <
rafadurancastaneda at gmail.com> escribió:

> I think you might use a tuple instead of a list for OrderElement, that
> would make much easier your code:
>
> class
> OrderElement(tuple):
>
>     def __new__(cls, x, y):
>         if not isinstance(x, int) or not isinstance(y, int):
>             raise TypeError("Order element must receives two
> integers")
>
>         return tuple.__new__(cls, (x, y))
>
>
> class Order(list):
>     def __setitem__(self, item):
>         assert isinstance(item, OrderElement)
>         super(Order, self).__setitem__(item)
>
>
> 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.
>
>
> 2011/8/7 Steven D'Aprano <steve+comp.lang.python at pearwood.info>
>
>> Roy Smith wrote:
>>
>> > In article <mailman.2010.1312731312.1164.python-list at python.org>,
>> >  John O'Hagan <research at johnohagan.com> wrote:
>> >
>> >> I'm looking for good ways to ensure that attributes are only writable
>> >> such that they retain the characteristics the class requires.
>> >
>> > Sounds like you're trying to do
>> > http://en.wikipedia.org/wiki/Design_by_contract.  Which is not a bad
>> > thing.  But, I think a more pythonic way to implement this would be to
>> > verify behaviors, not types.
>> >
>> > I would start by writing a assert_invarient() method which validates the
>> > object.  I'm guessing all you really need is that you can index [0] and
>> > [1] and get ints, so test for that.  Something like:
>> >
>> > def assert_invarient(self):
>> >    try:
>> >       assert isinstance(data[0], int)
>> >       assert isinstance(data[1], int)
>> >    except:
>> >       raise ValueError
>>
>> Don't do that. assert is for testing program logic, not verifying data.
>> The
>> problem with assert is that the user can turn all assertions off, simply
>> by
>> launching Python with the -O switch. Your verification code then becomes:
>>
>> def assert_invarient(self):
>>    try:
>>         pass
>>    except:
>>        raise ValueError
>>
>> which is useless.
>>
>> When should you use an assertion? If you've ever written code like this:
>>
>> if condition:
>>    do_something()
>> else:
>>    # This should never happen. But you know what they say: code that
>>    # can't happen, does!
>>    raise RuntimeError('condition unexpectedly false')
>>
>>
>> that's a prime candidate for turning into an assertion:
>>
>>
>> assert condition, 'condition unexpectedly false'
>> do_something()
>>
>>
>>
>> --
>> Steven
>>
>> --
>> http://mail.python.org/mailman/listinfo/python-list
>>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20110807/e44fb2c6/attachment.html>


More information about the Python-list mailing list