Exception as the primary error handling mechanism?

Roel Schroeven rschroev_nospam_ml at fastmail.fm
Thu Jan 7 12:43:14 CET 2010

Bruno Desthuilliers schreef:
> Phlip a écrit :
>> On Jan 5, 8:49 pm, Steven D'Aprano
>> <ste... at REMOVE.THIS.cybersource.com.au> wrote:
>>>> (A related question - why can't I just go 'if record = method():  use
>>>> (record)'. Why extra lines just to trap and assign the variable before
>>>> using it?)
>>> Because that idiom is responsible for probably the most common error in C
>>> of all, at least one of the most common errors. Thank goodness Python
>>> forbids such a dangerous construct.
>> switching = for == is the "most common error in C"?
>> I can't tell if you are joking.
> It's at least a _very_ common error in all languages that allow this 
> construct.

Maybe it is, maybe it's not. All I know is my own experience; in all the
years I've been doing C and C++ (1998 - now) I've made that mistake only
twice. And in both cases I found the mistake very rapidly.

> In C, it's common enough to gave birth to the "BestPractice" 
> you described, ie swapping operand orders in equality test to have the 
> compiler detect the problem - at least when one of the operand is a 
> function call expression or constant (it obviously won't 'work' when 
> both operands are variables).

I've never liked that practice, for the following reasons:
- As you say, it doesn't work when both operands are variables. In my
experience, in many cases both operands are variables.
- I tend to think that not following that practice trains me to be
careful in all cases, whereas I'm afraid that following the practice
will make me careless, which is dangerous in all the cases where the
practice won't protect me.

> Anyway: in Python, assignment is not an expression, and this isn't going 
> to change anytime soon.

To be fully clear: I'm not advocating to change the current behavior in
Python, I'm just stating my experience in other languages.

The saddest aspect of life right now is that science gathers knowledge
faster than society gathers wisdom.
  -- Isaac Asimov

Roel Schroeven

More information about the Python-list mailing list