"Angus Rodgers" <ang...@bigfoot.com> wrote

You can miss out a few else clauses, especially after
the try/excepts:

-----------
while get_bool("Continue?"):
try: key = eval(raw_input("Key value of type " + key_typ.__name__ + ": "))
   except StandardError: print "That's not a Python expression!"

   if not isinstance(key, key_typ):
print "That's not a value of type", key_typ.__name__ else: # User has provided try: val = eval(raw_input("Object of type " + val_typ.__name__ + ": "))
   except StandardError: print "That's not a Python expression!"

I don't see this, because, if an exception does occur, won't
there be an attempt to access one of the objects 'key', 'val'
before it has been assigned?

There will, which will throw another exception and exit.
And the exception will tell you which try failed, which is
more than your generic printed error message does...

Sometimes just letting Python fail is the best course.
Of course your messages will be prettier than the standard
stacktrace, but less useful for debugging.

However you can get what you want without the else by
simply inserting a break in each exception handler, which
keeps the indentation under control.

while get_bool("Continue?"):
try: key = eval(raw_input("Key value of type " + key_typ.__name__ + ": "))
   except StandardError:
       print "That's not a Python expression!"
       break

   if not isinstance(key, key_typ):
print "That's not a value of type", key_typ.__name__ else: # User has provided
      etc...

HTH,

--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/

_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to