[Tutor] Input validation

Alan Gauld alan.gauld at btinternet.com
Fri Sep 4 02:00:20 CEST 2009


"Albert-Jan Roskam" <fomcl at yahoo.com> wrote

> I'm wondering what is the most common method of input validation. 

Any and all of the methods will work in different situations but its 
generally best to use duck typing and try to write code that works 
for any type as far as possible.

Then use exceptions to catch the ,erm, exceptions...

So you could do

try: param.readlines()
except AttributeError:   # not a file like object
      try param.uppercase()
      except AttributeError:   # not a string


And so on.

HTH,


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



More information about the Tutor mailing list