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


Groups > comp.lang.python > #21532

Re: Raise X or Raise X()?

Date 2012-03-12 13:06 +0000
From James Elford <fil.oracle@gmail.com>
Subject Re: Raise X or Raise X()?
References <387014.2537.1331492695725.JavaMail.geo-discussion-forums@pbjv6>
Newsgroups comp.lang.python
Message-ID <mailman.585.1331557597.3037.python-list@python.org> (permalink)

Show all headers | 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