Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!border2.nntp.ams2.giganews.com!border4.nntp.ams.giganews.com!border2.nntp.ams.giganews.com!nntp.giganews.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'attributes': 0.05; '>>': 0.07; 'happen,': 0.07; 'python': 0.08; '[0]': 0.09; 'assert': 0.09; 'tuple': 0.09; 'writable': 0.09; 'def': 0.15; '(x,': 0.16; 'except:': 0.16; 'guessing': 0.16; 'int)': 0.16; 'int):': 0.16; 'pythonic': 0.16; 'repost': 0.16; 'roy': 0.16; 'subject:writing': 0.16; 'switch.': 0.16; 'unexpectedly': 0.16; 'useless.': 0.16; 'valueerror': 0.16; 'verifying': 0.16; '\xa0john': 0.16; '\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0': 0.16; 'this:': 0.16; 'written': 0.16; 'wrote:': 0.16; 'candidate': 0.21; 'trying': 0.21; 'email addr:gmail.com>': 0.21; 'header:In- Reply-To:1': 0.22; 'happen.': 0.23; 'testing': 0.24; 'index': 0.24; 'receives': 0.24; 'code': 0.25; 'url:wiki': 0.25; '>': 0.26; "i'm": 0.27; 'raise': 0.28; 'problem': 0.28; 'url:mailman': 0.28; 'pass': 0.29; 'message-id:@mail.gmail.com': 0.29; 'second': 0.29; 'module': 0.30; 'object.': 0.30; 'types.': 0.30; 'class': 0.30; '\xa0\xa0\xa0': 0.31; "didn't": 0.31; 'list': 0.32; 'implement': 0.32; "isn't": 0.33; "can't": 0.33; 'to:addr:python- list': 0.33; 'instead': 0.33; 'url:listinfo': 0.33; '...': 0.34; 'but,': 0.34; 'quite': 0.34; 'test': 0.34; 'like:': 0.34; 'retain': 0.34; 'try:': 0.34; 'from:charset:iso-8859-1': 0.35; 'url:python': 0.36; 'data.': 0.36; 'class.': 0.37; 'element': 0.37; 'skip:& 60': 0.37; 'but': 0.37; 'something': 0.37; 'two': 0.37; 'could': 0.38; 'think': 0.38; 'steven': 0.38; 'received:google.com': 0.38; 'url:org': 0.38; 'received:209.85': 0.38; 'should': 0.38; 'easier': 0.38; 'skip:o 20': 0.38; 'subject:: ': 0.39; 'skip:\xa0 10': 0.39; 'url:en': 0.39; 'ways': 0.39; 'user': 0.39; 'to:addr:python.org': 0.39; 'might': 0.40; 'more': 0.60; 'your': 0.61; '8bit%:92': 0.61; 'order': 0.62; 'john': 0.62; 'ever': 0.65; '8bit%:90': 0.67; 'prime': 0.67; 'verification': 0.76; 'article': 0.76; 'condition,': 0.84; 'does!': 0.84; '\xa0but,': 0.84; "\xa0i'm": 0.84; 'rafael': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=QnGXv86/k7qnlFT5/NLO7tpTM0NJDmCvtVuMc1x2/kc=; b=H9suMeMmF+buhitkWWq+vIeOHrkv/Lb00jt7Brs1SfNCLZcwtZN7PuxTjClldaZzln y+6WIeKUvm11Ebz7RNfKw7/BvBR46K/JxvCO50P7bV380XtMZxCIyBH3F+QLhZb6v1ie ImbP2CYFV3Ti5SG54a2iZVTWtb75SRwcD074g= MIME-Version: 1.0 In-Reply-To: References: <4e3ebdc8$0$29991$c3e8da3$5496439d@news.astraweb.com> Date: Sun, 7 Aug 2011 18:56:04 +0200 Subject: Re: Restricted attribute writing From: =?ISO-8859-1?Q?Rafael_Dur=E1n_Casta=F1eda?= To: python-list@python.org Content-Type: multipart/alternative; boundary=005045015f73c4652204a9ed3497 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 230 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1312736167 news.xs4all.nl 23970 [2001:888:2000:d::a6]:45143 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:11008 --005045015f73c4652204a9ed3497 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable 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=E1n Casta=F1eda < rafadurancastaneda@gmail.com> escribi=F3: > 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, bu= t > you could add a second condition two Order class. > > > 2011/8/7 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 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 t= he >> > object. I'm guessing all you really need is that you can index [0] an= d >> > [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 >> > > --005045015f73c4652204a9ed3497 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable 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=E1n Casta=F1eda <rafadurancastaneda@gmail.com> escribi=F3:
I think you might use a tuple instead of a = list for OrderElement, that would make much easier your code:

class= OrderElement(tuple):=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0= =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0= =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0= =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0= =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0= =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0= =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0= =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0= =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0= =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0= =A0
=A0=A0=A0 def __new__(cls, x, y):
=A0=A0=A0=A0=A0=A0=A0 if not isinstanc= e(x, int) or not isinstance(y, int):
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 r= aise TypeError("Order element must receives two integers")=A0=A0= =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0= =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0= =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0= =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0= =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0= =A0=A0=A0=A0
=A0=A0=A0=A0=A0=A0=A0 return tuple.__new__(cls, (x, y))


class Or= der(list):
=A0=A0=A0 def __setitem__(self, item):
=A0=A0=A0=A0=A0=A0= =A0 assert isinstance(item, OrderElement)
=A0=A0=A0=A0=A0=A0=A0 super(Or= der, 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@pearwood.info>
Roy Smith wrote:

> In article <mailman.2010.1312731312.1164.python-list@= python.org>,
> =A0John O'Hagan <research@johnohagan.com> wrote:
>
>> I'm looking for good ways to ensure that attributes are only w= ritable
>> such that they retain the characteristics the class requires.
>
> Sounds like you're trying to do
> http://en.wikipedia.org/wiki/Design_by_contract. =A0Which is no= t a bad
> thing. =A0But, 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 t= he
> object. =A0I'm guessing all you really need is that you can index = [0] and
> [1] and get ints, so test for that. =A0Something like:
>
> def assert_invarient(self):
> =A0 =A0try:
> =A0 =A0 =A0 assert isinstance(data[0], int)
> =A0 =A0 =A0 assert isinstance(data[1], int)
> =A0 =A0except:
> =A0 =A0 =A0 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):
=A0 =A0try:
=A0 =A0 =A0 =A0pass
=A0 =A0except:
=A0 =A0 =A0 =A0raise ValueError

which is useless.

When should you use an assertion? If you've ever written code like this= :

if condition:
=A0 =A0do_something()
else:
=A0 =A0# This should never happen. But you know what they say: code that =A0 =A0# can't happen, does!
=A0 =A0raise RuntimeError('condition unexpectedly false')


that's a prime candidate for turning into an assertion:


assert condition, 'condition unexpectedly false'
do_something()



--
Steven


--005045015f73c4652204a9ed3497--