Very Horrible Question About Goto's
John Mitchell
johnm at magnet.com
Wed Apr 19 10:44:53 EDT 2000
On Wed, 19 Apr 2000, Daley, MarkX wrote:
> It depends on what the GOTOs are doing. If they are providing a GOSUB-like
> feature, you can do the same thing in Python by defining the task and then
> invoking it in other functions. If they are providing flow control, it may
> be a little trickier.
> -----Original Message-----
> From: Richard Jones [mailto:richard.jones at ibahealthcare.com]
> Sent: Wednesday, April 19, 2000 12:09 AM
>
> I'm trying to create a converter from our own internal language (horrid)
> into python. My only problem so far is that the original language uses
> *LOTS* of goto's. Is there any way that this can be replicated within
> python ?
I have two suggestions:
1) the only valid use of goto's in C I've seen looks like this:
if (bogus)
goto cleanup
do stuff;
return(okay);
cleanup:
close file handles;
do cleanup
return(error)
One way to do this in Python is using Exceptions, which in practice is
much cleaner:
status = error
try:
do stuff
status = okay
except Bogus:
cleanup
return status
Also checkout try/finally for this type of thing.
2) Often, when porting a program from a procedural language (C, Perl) to
Python, you'd be better off *rewriting* the entire program. Yes, I know
it's a pain; but for some reason whenever I do this the code *shrinks* by
about 30-50%, and is more flexible and easier to read.
I used to write procedural code, then get sick of it after ~150 lines and
refactor it into Objects and Methods (ie: cut-and-paste). Resulting
program was shorter and much, much easier...
just my 2c
- j
More information about the Python-list
mailing list