[SLUG] Python questions

From: Paul M Foster (paulf@quillandmouse.com)
Date: Mon Aug 11 2003 - 18:09:37 EDT


Python programmers:

I'm having a couple of problems with python. First, surely there must be
a way to dump out of a program if some condition occurs, without having
to build all kinds of if's. Like:

def func():
        stuff
        if more_stuff:
                return 0
        else:
                return 1

# main program
if func():
        even_more_stuff
else:
        how_the_hell_do_I_get_out
less_stuff

In C and the like, you can simply execute a return or exit anywhere in
main(), but not in python.

The second problem is sometimes related to the first. Apparently, when
you open a file, as in:

open(filename, 'r')

if the file doesn't exist, you get this hairy error message and the
interpreter just dumps out. I've never seen this kind of behavior
before. I'd expect something more like

if open(filename, 'r'):
        do_stuff
else:
        couldnt_open_the_file_stuff

But that doesn't work. The only way I can see around this is to surround
every call to open() with a try/except block. That's clunky to me.
Anyone know something I don't?

Paul

-----------------------------------------------------------------------
This list is provided as an unmoderated internet service by Networked
Knowledge Systems (NKS). Views and opinions expressed in messages
posted are those of the author and do not necessarily reflect the
official policy or position of NKS or any of its employees.



This archive was generated by hypermail 2.1.3 : Fri Aug 01 2014 - 16:27:47 EDT