Re: try-except syntax
Steven D'Aprano wrote: imp.find_module is deprecated and should not be used in new code. ... try: block except (ImportError, RuntimeError): block Thanks Steven and others who replied. Looks more elegant. By the way, RuntimeError is almost never something you want to catch (except to log before bailing out). It should represent a fatal coding error, not something safe to ignore. I was simply playing around with 'imp'. Hence I blocked RuntimeError. -- https://mail.python.org/mailman/listinfo/python-list
Re: try-except syntax
On Thu, 05 Apr 2018 23:04:18 +0200, ElChino wrote: > I'm trying to simplify a try-except construct. E.g. how come this: >try: > _x, pathname, _y = imp.find_module (mod, mod_path) > return ("%s" % pathname) imp.find_module is deprecated and should not be used in new code. Putting that aside, pathname is already a string. Why are you wasting time interpolating it into a bare "%s" string? Just say `return pathname`. >except ImportError: > pass >except RuntimeError: > pass > return ("") Unnecessary parens surrounding a single value. Possible indentation error. Surely the return "" needs to be indented level with the try/except statements? > Cannot be simplified into this: >try: > _x, pathname, _y = imp.find_module (mod, mod_path) return ("%s" % > pathname) >except ImportError: >except RuntimeError: > pass > return ("") > > Like a "fall-through" in a C-switch statement. Because fall-through C switch syntax is an abomination, and fortunately the designer of Python has more sense than to allow that awfulness. The syntax you are looking for is: try: block except (ImportError, RuntimeError): block By the way, RuntimeError is almost never something you want to catch (except to log before bailing out). It should represent a fatal coding error, not something safe to ignore. -- Steve -- https://mail.python.org/mailman/listinfo/python-list
Re: try-except syntax
Ben Bacarisse wrote: Anyway, to coalesce two or more exception handlers, you are probably looking for except (ImportError, RuntimeError): To the OP: Are you sure you really want to mask RuntimeError here? Usually it means something has gone seriously wrong, and is a symptom that shouldn't be ignored. -- Greg -- https://mail.python.org/mailman/listinfo/python-list
Re: try-except syntax
ElChinowrites: > I'm trying to simplify a try-except construct. E.g. how come > this: > try: > _x, pathname, _y = imp.find_module (mod, mod_path) > return ("%s" % pathname) > except ImportError: > pass > except RuntimeError: > pass > return ("") > > Cannot be simplified into this: > try: > _x, pathname, _y = imp.find_module (mod, mod_path) > return ("%s" % pathname) > except ImportError: > except RuntimeError: > pass > return ("") > > Like a "fall-through" in a C-switch statement. The except parts don't fall through, and each one needs a "suite". Also, the "pass" before the return is mysterious which makes me think there's an indent error in what you posted. Anyway, to coalesce two or more exception handlers, you are probably looking for except (ImportError, RuntimeError): which matches both. -- Ben. -- https://mail.python.org/mailman/listinfo/python-list
Re: try-except syntax
On Thu, Apr 5, 2018 at 3:04 PM, ElChinowrote: > I'm trying to simplify a try-except construct. E.g. how come > this: > try: > _x, pathname, _y = imp.find_module (mod, mod_path) > return ("%s" % pathname) > except ImportError: > pass > except RuntimeError: > pass > return ("") > > Cannot be simplified into this: > try: > _x, pathname, _y = imp.find_module (mod, mod_path) > return ("%s" % pathname) > except ImportError: > except RuntimeError: > pass > return ("") > > Like a "fall-through" in a C-switch statement. You're looking for: try: _x, pathname, _y = imp.find_module (mod, mod_path) return ("%s" % pathname) except (ImportError, RuntimeError): return ("") -- https://mail.python.org/mailman/listinfo/python-list
Re: try-except syntax
On 04/05/2018 02:04 PM, ElChino wrote: I'm trying to simplify a try-except construct. E.g. how come this: try: _x, pathname, _y = imp.find_module (mod, mod_path) return ("%s" % pathname) except ImportError: pass except RuntimeError: pass return ("") Cannot be simplified into this: try: _x, pathname, _y = imp.find_module (mod, mod_path) return ("%s" % pathname) except ImportError: except RuntimeError: pass return ("") Like a "fall-through" in a C-switch statement. try: _x, pathname, _y = imp.find_module (mod, mod_path) return ("%s" % pathname) except (ImportError, RuntimeError): pass return ("") That handles the identical case. C-style fall-throughs where you have one switch have just a bit of code before the fall-through kicks in (the slightly non-identical case) is often the source of disastrous code errors. -- Rob Gaddi, Highland Technology -- www.highlandtechnology.com Email address domain is currently out of order. See above to fix. -- https://mail.python.org/mailman/listinfo/python-list
try-except syntax
I'm trying to simplify a try-except construct. E.g. how come this: try: _x, pathname, _y = imp.find_module (mod, mod_path) return ("%s" % pathname) except ImportError: pass except RuntimeError: pass return ("") Cannot be simplified into this: try: _x, pathname, _y = imp.find_module (mod, mod_path) return ("%s" % pathname) except ImportError: except RuntimeError: pass return ("") Like a "fall-through" in a C-switch statement. -- https://mail.python.org/mailman/listinfo/python-list