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


Groups > comp.lang.python > #21532

Re: Raise X or Raise X()?

Path csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <fil.oracle@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.038
X-Spam-Evidence '*H*': 0.92; '*S*': 0.00; 'exceptions': 0.09; 'foo': 0.09; 'iterate': 0.09; 'exception': 0.12; 'def': 0.13; 'both.': 0.16; 'exceptions.': 0.16; 'wrote:': 0.18; '>>>': 0.18; 'header :In-Reply-To:1': 0.22; 'wonder': 0.23; 'stack': 0.24; 'preferred': 0.25; 'classes': 0.26; 'skip:[ 10': 0.27; 'raise': 0.28; "i'm": 0.28; 'yield': 0.29; 'class': 0.29; 'print': 0.29; '(and': 0.30; 'collections': 0.30; 'construct': 0.30; 'message-id:@gmail.com': 0.31; 'reflect': 0.31; 'skip:l 30': 0.32; "i've": 0.32; 'list': 0.32; 'header:User-Agent:1': 0.33; 'loop': 0.34; 'rather': 0.34; 'try:': 0.34; 'to:addr:python-list': 0.35; '...': 0.35; 'something': 0.35; 'received:209.85.214': 0.36; 'received:google.com': 0.37; 'skip:_ 10': 0.38; 'received:209.85': 0.38; 'some': 0.38; 'should': 0.38; 'except': 0.39; 'junk': 0.39; 'received:209': 0.39; 'to:addr:python.org': 0.40; 'here': 0.64; 'collection': 0.69; 'want,': 0.71; 'deeply': 0.74; 'it"': 0.84; 'number):': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=message-id:date:from:user-agent:mime-version:to:subject:references :in-reply-to:content-type:content-transfer-encoding; bh=Z+GkFAKj5gmkSx47IZrVsx5aXV8HayB8EMW5Q1ao1t0=; b=YMIzqdEx8On5832DRdxbn5x4R5rkSxo6dpB9Wo8TQvSYH9qld4qmyrkFSVPsmRivhc uMDAKzCYMuwi08nLba67r/4VTDFtB8YSUEPcZPHICkRVKpMZrDnnvQFgMOhUYWqK9Eux +Kuj9VNdUE/fxt6yG2dBCSD4Lhi4kseCSp9p/BRuWHEAr9ewmQH7degojTFFqxoct1Um 60HgOGFm2PI6dO+EBzsgM+ZpsbTsBk/CFWO92/lJHJyYGgHCnVtbr7mF7vLF50+3h7H/ HnWikaOX3hQIQoPkNQl5/I4hA1Pc+WcV8IBv3+X+MQORv+cmQt1dwW78DNAxnxX+09nQ ZNmw==
Date Mon, 12 Mar 2012 13:06:32 +0000
From James Elford <fil.oracle@gmail.com>
User-Agent Mozilla/5.0 (X11; Linux i686; rv:10.0.1) Gecko/20120216 Thunderbird/10.0.1
MIME-Version 1.0
To python-list@python.org
Subject Re: Raise X or Raise X()?
References <387014.2537.1331492695725.JavaMail.geo-discussion-forums@pbjv6>
In-Reply-To <387014.2537.1331492695725.JavaMail.geo-discussion-forums@pbjv6>
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding 7bit
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.585.1331557597.3037.python-list@python.org> (permalink)
Lines 51
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1331557597 news.xs4all.nl 6978 [2001:888:2000:d::a6]:39667
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:21532

Show key headers only | View raw


On 11/03/12 19:04, bvdp wrote:
> Which is preferred in a raise: X or X()? I've seen both. In my specific case I'm dumping out of a deep loop:
> 
> try:
>   for ...
>     for ...
>       for ...
>         if match:
>            raise StopInteration()
>          else ...
> 
> except StopInteration:
>    print "found it"

I wonder whether you need to use an exception here rather than a yield
statement? Exceptions should reflect Exceptional circumstances (and come
with associated stack trace, and so on...). The following should do
something like what you want, without raising exceptions.

>>> # Deeply loop into a collection of collections
>>> def find(collection):
...    for sub_col in collection:
...        for item in sub_col:
...            for foo in item.list_field:
...                if foo.is_match:
...                    yield foo

>>> # Some junk classes to iterate over
>>> class Item(object):
...    def __init__(self, some_range):
...        self.list_field = [ListedItem(i) for i in some_range]

>>> class ListedItem(object):
...    def __init__(self, number):
...        self.tag = number
...        self.is_match = False

>>>    def __str__(self):
...        return str(self.tag)

>>> # Construct a list of items
>>> l = [[Item(range(i)) for i in range(10)],
...	[Item(range(i, 2*i)) for i in range(10,20)]]

>>> l[0][9].list_field[3].is_match = True

>>> for i in find(l):
...     print(i)
3

James

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


Thread

Raise X or Raise X()? bvdp <bob@mellowood.ca> - 2012-03-11 12:04 -0700
  Re: Raise X or Raise X()? Irmen de Jong <irmen.NOSPAM@xs4all.nl> - 2012-03-11 21:37 +0100
    Re: Raise X or Raise X()? Chris Rebert <clp2@rebertia.com> - 2012-03-11 14:49 -0700
    Re: Raise X or Raise X()? Stefan Behnel <stefan_ml@behnel.de> - 2012-03-12 14:52 +0100
      Re: Raise X or Raise X()? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-03-12 15:08 +0000
        Re: Raise X or Raise X()? Stefan Behnel <stefan_ml@behnel.de> - 2012-03-12 17:08 +0100
  Re: Raise X or Raise X()? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-03-11 23:59 +0000
    Re: Raise X or Raise X()? bvdp <bob@mellowood.ca> - 2012-03-11 18:53 -0700
    Re: Raise X or Raise X()? MRAB <python@mrabarnett.plus.com> - 2012-03-12 02:26 +0000
  Re: Raise X or Raise X()? Jean-Michel Pichavant <jeanmichel@sequans.com> - 2012-03-12 11:37 +0100
  Re: Raise X or Raise X()? Robert Kern <robert.kern@gmail.com> - 2012-03-12 12:47 +0000
  Re: Raise X or Raise X()? James Elford <fil.oracle@gmail.com> - 2012-03-12 13:06 +0000
  Re: Raise X or Raise X()? Chris Angelico <rosuav@gmail.com> - 2012-03-13 00:13 +1100

csiph-web