"Angus Rodgers" <ang...@bigfoot.com> wrote
> (i) trapping the exception StandardError may, for all I know,
> be a daft thing to do;
Not daft but not that common. Mainly because use of eval()
is kind of frownedoon for security reasons. (See below.)
> (iv) correlatively with (iii), my indentation perhaps looks a
> little extreme (perhaps suggesting a redesign, I don't know);
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!"
if not isinstance(val, val_typ):
print "That's not an object of type", val_typ.__name__
else: # User has provided an object of the correct type
ans[key] = val
return ans
------------
I'm hoping you know about the security issues around using
eval with raw_input? The use can input any valid python code
and it will be evaluated! For experimenting its OK but in
live code it would be very risky.
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