On Sun, 26 Dec 2010 09:15:32 -0800, Ethan Furman wrote: > Steven D'Aprano wrote: >> Right. But I have thought of a clever trick to get the result KJ was >> asking for, with the minimum of boilerplate code. Instead of this: >> >> >> def _pre_spam(args): >> if condition(args): >> raise SomeException("message") >> if another_condition(args): >> raise AnotherException("message") >> if third_condition(args): >> raise ThirdException("message") >> >> def spam(args): >> _pre_spam(args) >> do_useful_work() >> >> >> you can return the exceptions instead of raising them (exceptions are >> just objects, like everything else!), and then add one small piece of >> boilerplate to the spam() function: >> >> >> def _pre_spam(args): >> if condition(args): >> return SomeException("message") >> if another_condition(args): >> return AnotherException("message") >> if third_condition(args): >> return ThirdException("message") >> >> def spam(args): >> exc = _pre_spam(args) >> if exc: raise exc >> do_useful_work() > > -1 > > You failed to mention that cleverness is not a prime requisite of the > python programmer -- in fact, it's usually frowned upon. The big > problem with the above code is you are back to passing errors in-band, > pretty much completely defeating the point of have an out-of-band > channel.
How is that any worse than making _pre_spam() a validation function that returns a bool? def spam(args): flag = _pre_spam(args) if flag: raise SomeException() do_useful_work() Is that also frowned upon for being too clever? -- Steven -- http://mail.python.org/mailman/listinfo/python-list