[Python-ideas] Introduce collections.Reiterable
Ryan
rymg19 at gmail.com
Sat Sep 21 22:02:36 CEST 2013
The problem with the try...except statement is that it'll catch errors that occur inside the function. Say the function calls zip and accidentally gives an int instead of their list. It'll raise a TypeError, which will be caught and a CallbackError will be raised. But, in some programs, that behavior doesn't work.
"Stephen J. Turnbull" <stephen at xemacs.org> wrote:
>Ryan writes:
>
> > I can still see why checking if its callable is a good idea in some
> > cases.
>
>You can always rewrite the LBYL in EAFP form:
>
> try:
> self.call[item]()
> except TypeError as e:
> raise CallbackError(...) from e
>
>This is definitely preferred if you can enclose a whole suite in a try
>and expect the CallbackError to be infrequent, eg:
>
> try:
> while item in input:
> self.call[item]()
> except TypeError as e:
> raise CallbackError(...) from e
>
>However, what you probably really want to do (which is a better
>argument for LBYL, anyway) is
>
> if callable(callback):
> self.call[item] = callback
> else:
> what_would_jruser_do()
--
Sent from my Android phone with K-9 Mail. Please excuse my brevity.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20130921/2b7be30f/attachment.html>
More information about the Python-ideas
mailing list