Re: The Most Diabolical Python Antipattern

2015-01-30 Thread Mark Lawrence
On 30/01/2015 08:10, Mark Lawrence wrote: On 30/01/2015 06:16, Marko Rauhamaa wrote: Ian Kelly : At least use "except Exception" instead of a bare except. Do you really want things like SystemExit and KeyboardInterrupt to get turned into 0? How about: == try

Re: The Most Diabolical Python Antipattern

2015-01-30 Thread Ian Kelly
On Fri, Jan 30, 2015 at 8:56 AM, Marko Rauhamaa wrote: > Ian Kelly : > >> Like I suggested earlier, just don't catch the inner exception at all. >> The result will be both exceptions propagated, chained in the proper >> order. > > Depends on the situation. Like what? If you want to specifically p

Re: The Most Diabolical Python Antipattern

2015-01-30 Thread Marko Rauhamaa
Ian Kelly : > Like I suggested earlier, just don't catch the inner exception at all. > The result will be both exceptions propagated, chained in the proper > order. Depends on the situation. Marko -- https://mail.python.org/mailman/listinfo/python-list

Re: The Most Diabolical Python Antipattern

2015-01-30 Thread Chris Angelico
On Sat, Jan 31, 2015 at 2:42 AM, Ian Kelly wrote: > Like I suggested earlier, just don't catch the inner exception at all. > The result will be both exceptions propagated, chained in the proper > order. So many MANY times, the best thing to do with unrecognized exceptions is simply to not catch t

Re: The Most Diabolical Python Antipattern

2015-01-30 Thread Ian Kelly
On Fri, Jan 30, 2015 at 8:30 AM, Marko Rauhamaa wrote: > Ian Kelly : > >> The bare raise re-raises the most recent exception that is being >> handled. The "raise e" raises that exception specifically, which is >> not the most recent in the case of a secondary exception. > > Scary. That affects all

Re: The Most Diabolical Python Antipattern

2015-01-30 Thread Marko Rauhamaa
Ian Kelly : > The bare raise re-raises the most recent exception that is being > handled. The "raise e" raises that exception specifically, which is > not the most recent in the case of a secondary exception. Scary. That affects all finally clauses. Must remember that. The pitfall is avoided by

Re: The Most Diabolical Python Antipattern

2015-01-30 Thread Ian Kelly
On Fri, Jan 30, 2015 at 3:00 AM, Marko Rauhamaa wrote: > Marko Rauhamaa : > Surprisingly this variant could raise an unexpected exception: == try: do_interesting_stuff() except ValueError: try:

Re: The Most Diabolical Python Antipattern

2015-01-30 Thread Marko Rauhamaa
Marko Rauhamaa : >>> Surprisingly this variant could raise an unexpected exception: >>> >>> == >>> try: >>> do_interesting_stuff() >>> except ValueError: >>> try: >>> log_it() >>> finally: >>> raise >>>

Re: The Most Diabolical Python Antipattern

2015-01-30 Thread Ian Kelly
On Fri, Jan 30, 2015 at 2:02 AM, Marko Rauhamaa wrote: > Mark Lawrence : > >> On 30/01/2015 06:16, Marko Rauhamaa wrote: >>> How about: >>> >>> == >>> try: >>> do_interesting_stuff() >>> except ValueError: >>> try: >>> log_it() >

Re: The Most Diabolical Python Antipattern

2015-01-30 Thread Ian Kelly
On Thu, Jan 29, 2015 at 11:16 PM, Marko Rauhamaa wrote: > Ian Kelly : > >> At least use "except Exception" instead of a bare except. Do you >> really want things like SystemExit and KeyboardInterrupt to get turned >> into 0? > > How about: > > == > try: > do

Re: The Most Diabolical Python Antipattern

2015-01-30 Thread Marko Rauhamaa
Mark Lawrence : > On 30/01/2015 06:16, Marko Rauhamaa wrote: >> How about: >> >> == >> try: >> do_interesting_stuff() >> except ValueError: >> try: >> log_it() >> except: >> pass >> raise >> ===

Re: The Most Diabolical Python Antipattern

2015-01-30 Thread Mark Lawrence
On 30/01/2015 06:16, Marko Rauhamaa wrote: Ian Kelly : At least use "except Exception" instead of a bare except. Do you really want things like SystemExit and KeyboardInterrupt to get turned into 0? How about: == try: do_interesting_stuff() exce

Re: The Most Diabolical Python Antipattern

2015-01-29 Thread jtan
I would usually just log the stack trace so that I would still know that something bad happened while the app looks okay. On Fri, Jan 30, 2015 at 1:17 AM, Mark Lawrence wrote: > The author is quite clear on his views here https://realpython.com/blog/ > python/the-most-diabolical-python-antipatte

Re: The Most Diabolical Python Antipattern

2015-01-29 Thread Michiel Overtoom
On Jan 29, 2015, at 18:36, Skip Montanaro wrote: > There are the occasional instance where I need to recover > from an exception no matter what caused it. I had the same need, a while ago, when working on a CherryPy webapp which uses a BackgroundTask to parse user-uploaded data and image files

Re: The Most Diabolical Python Antipattern

2015-01-29 Thread Marko Rauhamaa
Ian Kelly : > At least use "except Exception" instead of a bare except. Do you > really want things like SystemExit and KeyboardInterrupt to get turned > into 0? How about: == try: do_interesting_stuff() except ValueError: try: log_

Re: The Most Diabolical Python Antipattern

2015-01-29 Thread Chris Angelico
On Fri, Jan 30, 2015 at 4:32 AM, Tim Chase wrote: > I have one that I call int0() > that is my "give me a freakin' int" function which is something like > > def int0(val): > try: > return int(val) > except: > return 0 > > because I deal with a lot of CSV data from client/vend

Re: The Most Diabolical Python Antipattern

2015-01-29 Thread Ian Kelly
On Thu, Jan 29, 2015 at 10:32 AM, Tim Chase wrote: > On 2015-01-29 17:17, Mark Lawrence wrote: >> The author is quite clear on his views here >> https://realpython.com/blog/python/the-most-diabolical-python-antipattern/ >> but what do you guys and gals think? > > I just read that earlier today and

Re: The Most Diabolical Python Antipattern

2015-01-29 Thread Tim Chase
On 2015-01-29 17:17, Mark Lawrence wrote: > The author is quite clear on his views here > https://realpython.com/blog/python/the-most-diabolical-python-antipattern/ > but what do you guys and gals think? I just read that earlier today and agree for the most part. The only exception (pun only pa

Re: The Most Diabolical Python Antipattern

2015-01-29 Thread Ethan Furman
On 01/29/2015 09:36 AM, Skip Montanaro wrote: > On Thu, Jan 29, 2015 at 11:17 AM, Mark Lawrence > wrote: >> >> ... but what do you guys and gals think? > > I saw that blog referenced elsewhere a day or two ago. I think he's > correct. There are the occasional instance where I need to recover > f

Re: The Most Diabolical Python Antipattern

2015-01-29 Thread Skip Montanaro
On Thu, Jan 29, 2015 at 11:17 AM, Mark Lawrence wrote: > > ... but what do you guys and gals think? I saw that blog referenced elsewhere a day or two ago. I think he's correct. There are the occasional instance where I need to recover from an exception no matter what caused it. In cases where I f