Re: Restricted attribute writing

2011-08-10 Thread Thomas Jollans
On 07/08/11 17:35, John O'Hagan wrote: > I'm looking for good ways to ensure that attributes are only writable such > that they retain the characteristics the class requires. > > My particular case is a class attribute which is initialised as a list of > lists of two integers, the first of whic

Re: Restricted attribute writing

2011-08-07 Thread John O'Hagan
On Mon, 08 Aug 2011 03:07:30 +1000 Steven D'Aprano wrote: > John O'Hagan wrote: > > > I'm looking for good ways to ensure that attributes are only writable such > > that they retain the characteristics the class requires. > > That's what properties are for. > > > My particular case is a class

Re: Restricted attribute writing

2011-08-07 Thread Chris Angelico
On Sun, Aug 7, 2011 at 6:07 PM, Steven D'Aprano wrote: > class ThingWithTwoIntegers(object): > I'm not a lisp expert, but this might well be called a cons cell. Or a "pair". ChrisA -- http://mail.python.org/mailman/listinfo/python-list

Re: Restricted attribute writing

2011-08-07 Thread Steven D'Aprano
John O'Hagan wrote: > I'm looking for good ways to ensure that attributes are only writable such > that they retain the characteristics the class requires. That's what properties are for. > My particular case is a class attribute which is initialised as a list of > lists of two integers, the fir

Re: Restricted attribute writing

2011-08-07 Thread Rafael Durán Castañeda
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 < rafadurancastan...@gmail.com> escribió: > I think you might use a tuple instead of a list for OrderElement, that > would make much easier your

Re: Restricted attribute writing

2011-08-07 Thread Rafael Durán Castañeda
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")

Re: Restricted attribute writing

2011-08-07 Thread Steven D'Aprano
Roy Smith wrote: > In article , > John O'Hagan 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

Re: Restricted attribute writing

2011-08-07 Thread Roy Smith
In article , John O'Hagan 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

Restricted attribute writing

2011-08-07 Thread John O'Hagan
I'm looking for good ways to ensure that attributes are only writable such that they retain the characteristics the class requires. My particular case is a class attribute which is initialised as a list of lists of two integers, the first of which is a modulo remainder. I need to be able to wr