Re: [Tutor] Stack unwind using exceptions.
On Sunday 05 July 2009, Noufal Ibrahim wrote: > Kent Johnson wrote: > [..] > > > Why not just return the value from the function and pass it up > > the call chain? If a call fails return None. Something like this: > > That's what I ended up doing but the first thing occurred to me and > I was just wondering if there's any production code that relies on > the technique. The Pyparsing library uses exceptions a lot internally. If I understood it right, exceptions are used to tell that a pattern does not match. If the pattern matches the results are transported with a regular 'return'. As it happens quite often that a pattern does not match, exceptions can be considered a regular mechanism for information transport in Pyparsing. There are two types of exceptions in Pyparsing. An exception that means: try the next pattern; and there are exceptions that mean: there was a fatal error, stop parsing. Pyparsing: http://pyparsing.wikispaces.com/ Kind regards, Eike. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Stack unwind using exceptions.
Noufal Ibrahim wrote: Kent Johnson wrote: [..] Why not just return the value from the function and pass it up the call chain? If a call fails return None. Something like this: That's what I ended up doing but the first thing occurred to me and I was just wondering if there's any production code that relies on the technique. I use the exception technique in my Python Pipelines parser. The recursive routine has 6 raise statements. The exceptions are caught by the program that calls the recursive routine. There would be more code and complexity to work back up the call chain. -- Bob Gailer Chapel Hill NC 919-636-4239 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Stack unwind using exceptions.
Kent Johnson wrote: [..] Why not just return the value from the function and pass it up the call chain? If a call fails return None. Something like this: That's what I ended up doing but the first thing occurred to me and I was just wondering if there's any production code that relies on the technique. -- ~noufal http://nibrahim.net.in/ ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Stack unwind using exceptions.
On Sat, Jul 4, 2009 at 3:56 PM, Noufal Ibrahim wrote: > Hello everyone, > > Would it be considered unorthodox or 'wrong' in some sense for me to use an > exception to unwind the stack and return to a caller for a recursive > function? > > I need to do a depth first search and when I find the right element to just > stop the whole thing, and get back to the caller saying that I found it. Why not just return the value from the function and pass it up the call chain? If a call fails return None. Something like this: def search(self, value): if self.value == value: return self for child in self.children: result = child.search(value) if result is not None: return result return None Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Stack unwind using exceptions.
Hello everyone, Would it be considered unorthodox or 'wrong' in some sense for me to use an exception to unwind the stack and return to a caller for a recursive function? I need to do a depth first search and when I find the right element to just stop the whole thing, and get back to the caller saying that I found it. TIA. -- ~noufal http://nibrahim.net.in/ ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor