[EMAIL PROTECTED] wrote: > Hi everyone, > > Maybe these questions will sound strange to you, but I sometime have a > hard time switching from Java to Python ;-) > > Let's say I have a function like this : > > def show_lines(file): > for next_line in file: > ... > > What can I do to be sure that the input argument is indeed a 'File' > object ? > > #1 : should I start by checking that 'file' is indeed an instance of a > File object ? (and how do I do this ?) > #2 : should I do nothing ? (but I don't like the idea of risking to > have a runtime exception raised somewhere) >
It depends how you want to do your exception handling. Assuming you have passed in an object that isn't a file, how do you want to handle it ? Here is an example : class ProgrammerError(Exception): pass def function(myFile): if not isinstance(myFile, file): raise ProgrammerError for line in myFile: print myFile try: function('something') except ProgrammerError: print 'I did something wrong' Writing tests that check your code doesn't do this would be a better approach. This is 'strong testing' as opposed to 'strong typing'. Fuzzyman http://www.voidspace.org.uk/python/index.shtml > Thanks for helping... -- http://mail.python.org/mailman/listinfo/python-list