Re: Customize the effect of enumerate()?



Fredrik Lundh wrote:
"Dustan" wrote:

Except that my program is supposed to be treated as a module with tools
to do certain things. I certainly can't control whether a 3rd party
programmer uses "import myModule" or "from myModule import *".

anything can happen if people use "from import *" in the wrong way, so that's
not much of an argument, really.

</F>

My argument was that if they use "import myModule", overriding
enumerate() wouldn't work. So "from myModule import *" would work
nicely, but not the former. Given that, I'm not getting your rebuttal,
or whatever it is.


Steven D'Aprano wrote:
On Sun, 22 Oct 2006 15:56:16 -0700, Simon Forman wrote:
That final else clause is a little funny... What kind of indices are
you expecting that will be neither less than zero, greater than zero,
or equal to zero?

Possible a NaN value? Maybe a class instance with strange comparison
methods?

Personally, I don't like the error message. "Invalid index" doesn't really
tell the caller what went wrong and why it is an invalid index. If I were
programming that defensively, I'd write:

if item > 0:
return self.sequence[item-1]
elif item < 0:
return self.sequence[item]
elif item == 0:
raise IndexError, "Index 0 is not valid."
else:
print repr(item)
raise ThisCanNeverHappenError("Congratulations! You've discovered "
"a bug that can't possibly occur. Contact the program author for "
"your reward.")

I know some programmers hate "Can't happen" tests and error messages, but
if you are going to test for events that can't possibly happen, at least
deal with the impossible explicitly!

I certainly can't argue with that logic; I might even go so far as to
agree with you and start raising impossible errors with this kind of
explicitness.

What reward should I offer? ;-)

.



Relevant Pages

  • Re: handling of errors
    ... Checking them is part of the job of programming. ... out an error message and exit (hopefully with a 'non-successful' ... IMO, asking the operator is the lazy ... code by removing meaningful error messages and replacing them ...
    (bit.listserv.ibm-main)
  • Re: How to avoid searching this folder?
    ... You should declare variables where you first use them and you should ... in the error message so you know why it failed: ... Why would $p have zero length? ... length string if there is leading whitespace in $line. ...
    (comp.lang.perl.misc)
  • Re: Zero values cause errors on log scale
    ... >>If these zero values are "real" then a log scale is not appropriate, ... > The Excel error message is infuriating because it cannot be disabled on ... > a per plot basis. ... > Martin Brown ...
    (microsoft.public.excel.charting)
  • Re: silly digital specs on analog parts
    ... and DAC registers are cleared to zero at the falling edge of ... forcing the analog outputs to zero scale. ... I've found that any error message results because I did something ...
    (sci.electronics.design)
  • Re: Exception Handling and Refactoring Question ...
    ... print a error message and abort. ... > writing a single line of code (my programming skills were much inferior ... Exception programming can be used to facilitate automatic ... useful software I ever saw is vulnerable to incorrect user input. ...
    (comp.lang.cpp)