<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div>On Sep 21, 2013, at 13:02, Ryan <<a href="mailto:rymg19@gmail.com">rymg19@gmail.com</a>> wrote:</div><div><br></div><blockquote type="cite"><div>The problem with the try...except statement is that it'll catch errors that occur inside the function.</div></blockquote><blockquote type="cite"><div>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. </div></blockquote><div><br></div><div>You can always use EAFP with a bit of "look back after you leaped" to help diagnose the error:</div><div><span style="font-family: sans-serif; -webkit-tap-highlight-color: rgba(26, 26, 26, 0.292969); -webkit-composition-fill-color: rgba(175, 192, 227, 0.230469); -webkit-composition-frame-color: rgba(77, 128, 180, 0.230469); "><br></span></div><div><span style="font-family: sans-serif; -webkit-tap-highlight-color: rgba(26, 26, 26, 0.292969); -webkit-composition-fill-color: rgba(175, 192, 227, 0.230469); -webkit-composition-frame-color: rgba(77, 128, 180, 0.230469); ">try:</span></div><div><span style="font-family: sans-serif; -webkit-tap-highlight-color: rgba(26, 26, 26, 0.292969); -webkit-composition-fill-color: rgba(175, 192, 227, 0.230469); -webkit-composition-frame-color: rgba(77, 128, 180, 0.230469); ">    self.call[item]()</span></div><div><span style="font-family: sans-serif; -webkit-tap-highlight-color: rgba(26, 26, 26, 0.292969); -webkit-composition-fill-color: rgba(175, 192, 227, 0.230469); -webkit-composition-frame-color: rgba(77, 128, 180, 0.230469); ">except TypeError as e:</span></div><div><span style="font-family: sans-serif; -webkit-tap-highlight-color: rgba(26, 26, 26, 0.292969); -webkit-composition-fill-color: rgba(175, 192, 227, 0.230469); -webkit-composition-frame-color: rgba(77, 128, 180, 0.230469); ">    if not callable(self.call[item]):</span></div><div><span style="font-family: sans-serif; -webkit-tap-highlight-color: rgba(26, 26, 26, 0.292969); -webkit-composition-fill-color: rgba(175, 192, 227, 0.230469); -webkit-composition-frame-color: rgba(77, 128, 180, 0.230469); ">        raise CallbackError(...) from e</span></div><div>    raise</div><div><br></div><div>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).</div><div><br></div><div>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.</div><br><blockquote type="cite"><div>But, in some programs, that behavior doesn't work.<br><br><div class="gmail_quote">"Stephen J. Turnbull" <<a href="mailto:stephen@xemacs.org">stephen@xemacs.org</a>> wrote:<blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
<pre style="white-space: pre-wrap; word-wrap:break-word; font-family: sans-serif; margin-top: 0px">Ryan writes:<br><br><blockquote class="gmail_quote" style="margin: 0pt 0pt 1ex 0.8ex; border-left: 1px solid #729fcf; padding-left: 1ex;">I can still see why checking if its callable is a good idea in some<br>cases.</blockquote><br>You can always rewrite the LBYL in EAFP form:<br><br>try:<br>self.call[item]()<br>except TypeError as e:<br>raise CallbackError(...) from e<br><br>This is definitely preferred if you can enclose a whole suite in a try<br>and expect the CallbackError to be infrequent, eg:<br><br>try:<br>while item in input:<br>self.call[item]()<br>except TypeError as e:<br>raise CallbackError(...) from e<br><br>However, what you probably really want to do (which is a better<br>argument for LBYL, anyway) is<br><br>if callable(callback):<br>self.call[item] = callback<br>else:<br>what_would_jruser_do()<br><br></pre></blockquote></div><br>
-- <br>
Sent from my Android phone with K-9 Mail. Please excuse my brevity.</div></blockquote><blockquote type="cite"><div><span>_______________________________________________</span><br><span>Python-ideas mailing list</span><br><span><a href="mailto:Python-ideas@python.org">Python-ideas@python.org</a></span><br><span><a href="https://mail.python.org/mailman/listinfo/python-ideas">https://mail.python.org/mailman/listinfo/python-ideas</a></span><br></div></blockquote></body></html>