<div dir="ltr"><div><div><div><div><div><div><div><div><div><div>Hi all,<br><br></div>a lot of times the following pattern pops out in Python code:<br><br></div>try:<br></div> somecode<br></div>except SomeException:<br></div> pass<br><br></div><div></div>A very simple example could be if you want to process a list that may  be empty<br><br></div><div>def process_list(lst):<br></div> try:<br></div>  lst[0] = lst[0] + 1<br></div> except IndexError:<br></div>  pass<br><div><div><div><div><div><div><div><div><div><div><br></div><div>or in more complex cases in which however an exception just signals that there is nothing to do there.<br><br></div><div>Converting the code to a non-EAFP version, for example<br><br></div><div>if len(lst) != 0:<br> lst[0] = lst[0] + 1<br><br></div><div>is in my opinion generally against the Python nature, since it relies on a specific check on the object, instead of trusting the object as being able to either satisfy the request or raise an exception. That is, this solution is non-polymorphic.<br><br>In such cases sometimes ABC may help, but generally speaking they it is not always the case of being an instance of a given ABC or not. The problem here is if the code raises an exception or not.<br><br></div><div></div><div>Would it be feasible to propose a short syntax like this?<br><br></div><div>pass SomeException:<br>  somecode</div><div><br></div><div>where the above example would become:<br><br></div><div>pass IndexError:<br> lst[0] = lst[0] + 1</div><div><br></div><div>I could not find if such a syntax has been already discussed elsewhere, so please let me know if this is the case.<br><br>Otherwise, what do you think about it?<br><br></div><div>Thank you<br><br></div><div>Leonardo<br></div><div><br></div><div><br><br clear="all"><div><div><div dir="ltr"><div>Leonardo Giordani<br><a href="http://twitter.com/tw_lgiordani" target="_blank">@tw_lgiordani</a> - <a href="http://lgiordani.com" target="_blank">lgiordani.com</a><br></div>My profile on <a href="http://about.me/leonardo.giordani" target="_blank">About.me</a> - My <a href="https://github.com/lgiordani" target="_blank">GitHub page</a><br></div></div>
</div></div></div></div></div></div></div></div></div></div></div></div>