[Python-ideas] Introduce collections.Reiterable
Ryan
rymg19 at gmail.com
Sun Sep 22 03:11:14 CEST 2013
At that rate, why not just check for callability(?) in the first place?
Andrew Barnert <abarnert at yahoo.com> wrote:
>On Sep 21, 2013, at 13:02, Ryan <rymg19 at gmail.com> wrote:
>
>> 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.
>
>You can always use EAFP with a bit of "look back after you leaped" to
>help diagnose the error:
>
>try:
> self.call[item]()
>except TypeError as e:
> if not callable(self.call[item]):
> raise CallbackError(...) from e
> raise
>
>I'm not sure that would be appropriate in this case, but similar code
>is very common when dealing with, e.g., the filesystem (especially in
>2.x, where you had to distinguish errors on errno... but even in 3.x
>it's often worth telling the user that his error was because the folder
>be specified doesn't exist, as opposed to just the filename being
>wrong).
>
>But anyway, I think this is way off topic for this thread. We already
>have both EAFP and LBYL mechanisms for dealing with iteration; the
>argument isn't which one you should use, but whether the existing ABCs
>are sufficient or leave an important gap if you choose to use them for
>LBYL.
>
>> 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.
>> _______________________________________________
>> Python-ideas mailing list
>> Python-ideas at python.org
>> https://mail.python.org/mailman/listinfo/python-ideas
--
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/6c42e457/attachment.html>
More information about the Python-ideas
mailing list