[Python-ideas] except expression

Chris Angelico rosuav at gmail.com
Wed Feb 19 03:20:32 CET 2014


On Wed, Feb 19, 2014 at 12:34 PM, Alexander Belopolsky
<alexander.belopolsky at gmail.com> wrote:
> On Tue, Feb 18, 2014 at 7:10 PM, Steven D'Aprano <steve at pearwood.info>
> wrote:
>>
>> > I disagree.  It is best to leave explicit selection of exceptions to
>> > catch
>> > outside of the expressions.  This is job for things like decimal or
>> > numpy
>> > fp contexts.
>>
>> I don't understand what relevance fp contexts have here.
>
>
>>>> numpy.seterr(divide='ignore')
> {'over': 'warn', 'divide': 'raise', 'invalid': 'warn', 'under': 'ignore'}
>>>> 1/numpy.array(0.0)
> inf
>>>> numpy.seterr(divide='raise')
> {'over': 'warn', 'divide': 'ignore', 'invalid': 'warn', 'under': 'ignore'}
>>>> 1/numpy.array(0.0)
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> FloatingPointError: divide by zero encountered in divide
>

That's solving the same problem in a slightly different way. Instead
of quickly trapping one exception right here, you set a state flag
that controls them globally. In a computationally-heavy program,
that's going to work out far FAR cleaner than this; but suppose most
of the time you want them to raise, and then you have one little
calculation in the middle where you don't. That's where this would
come in handy.

ChrisA


More information about the Python-ideas mailing list