Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #11008

Re: Restricted attribute writing

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 <rafadurancastaneda@gmail.com>
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; '&gt;&gt;': 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&gt;': 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; '&gt;': 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 <CAH+GN=0vP+JDS6+Ubp4oidr_ZR65BvN4-uK4aDzBcV8uLrteWQ@mail.gmail.com>
References <mailman.2010.1312731312.1164.python-list@python.org> <roy-0C3A61.12074507082011@news.panix.com> <4e3ebdc8$0$29991$c3e8da3$5496439d@news.astraweb.com> <CAH+GN=0vP+JDS6+Ubp4oidr_ZR65BvN4-uK4aDzBcV8uLrteWQ@mail.gmail.com>
Date Sun, 7 Aug 2011 18:56:04 +0200
Subject Re: Restricted attribute writing
From Rafael Durán Castañeda <rafadurancastaneda@gmail.com>
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 <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.2012.1312736167.1164.python-list@python.org> (permalink)
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

Show key headers only | View raw


[Multipart message — attachments visible in raw view] - view raw

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@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@pearwood.info>
>
>> Roy Smith wrote:
>>
>> > In article <mailman.2010.1312731312.1164.python-list@python.org>,
>> >  John O'Hagan <research@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
>>
>
>

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Restricted attribute writing John O'Hagan <research@johnohagan.com> - 2011-08-08 01:35 +1000
  Re: Restricted attribute writing Roy Smith <roy@panix.com> - 2011-08-07 12:07 -0400
    Re: Restricted attribute writing Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-08-08 02:31 +1000
      Re: Restricted attribute writing Rafael Durán Castañeda <rafadurancastaneda@gmail.com> - 2011-08-07 18:53 +0200
      Re: Restricted attribute writing Rafael Durán Castañeda <rafadurancastaneda@gmail.com> - 2011-08-07 18:56 +0200
  Re: Restricted attribute writing Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-08-08 03:07 +1000
    Re: Restricted attribute writing Chris Angelico <rosuav@gmail.com> - 2011-08-07 19:21 +0100
    Re: Restricted attribute writing John O'Hagan <research@johnohagan.com> - 2011-08-08 14:59 +1000

csiph-web