[Tutor] Is my style OK in this elementary student exercise?
Alan Gauld
alan.gauld at btinternet.com
Sat Jul 4 20:52:56 CEST 2009
"Angus Rodgers" <angusr at 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/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090704/42db1592/attachment-0001.htm>
More information about the Tutor
mailing list