"Stephen J. Turnbull" <step...@xemacs.org> writes: > Ethan Furman writes: > > On 02/21/2014 07:46 PM, Chris Angelico wrote: > > > > > > but not this: > > > > > > value = expr except Exception: default except Exception: default > > > > This should be the way it works. Nothing is gained in readability > > by turning a try with multiple except statements into an > > expression. > > Examples have been given several times. In general, if 'expr' is a > function call, it may well have a couple of different ways to fail > which imply different default values. > > interpolable = func(key) except TypeError: "not a string: %s" % key \ > except KeyError: "no such key: %s" % key > print("Some message that refers to '%s' % interpolable") > > versus > > try: > interpolable = func(key) > except TypeError: > interpolable = "not a string: %s" % key > except KeyError: > interpolable = "no such key: %s" % key > print("Some message that refers to '%s' % interpolable")
I think the following suggestion from elsewhere in the thread would look even better in this case: interpolable = func(key) except (TypeError: "not a string: %s" % key, KeyError: "no such key: %s" % key) print("Some message that refers to '%s' % interpolable") It does not require the backslash, it is shorter, and it can still be chained: interpolable = func(key) except (TypeError: "not a string: %s" % key, KeyError: defaults[key] except (KeyError: "no such key: %s" % key)) print("Some message that refers to '%s' % interpolable") Best, -Nikolaus -- Encrypted emails preferred. PGP fingerprint: 5B93 61F8 4EA2 E279 ABF6 02CF A9AD B7F8 AE4E 425C »Time flies like an arrow, fruit flies like a Banana.« _______________________________________________ Python-Dev mailing list Python-Dev@python.org https://mail.python.org/mailman/listinfo/python-dev Unsubscribe: https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com