Check if variable is an instance of a File object

Bruno Desthuilliers onurb at xiludom.gro
Fri Sep 15 08:30:27 EDT 2006


sc_wizard29 at hotmail.com wrote:
> Marc 'BlackJack' Rintsch a ecrit :
> 
>> And what do you do if you check for `file` and it isn't such an instance?
>> Raise an exception?  A no, that's something you don't like.  So what else?  ;-)
> 
> Well, I prefer the idea of raising my *own* exception to the idea of
> having an unknown behavior occur

The behavior in case of an uncaught exception is pretty well defined :
your app crashes with an error message and a traceback. Note that
nothing prevents you to try and catch most execptions at the top-level
and do whatever seems appropriate here (log the error, try to see if the
app just display the error and continue with another task or if it's
time to suicide, etc)...

> (for ex : what if the argument is NOT
> a File, but is "itereable" ?)

hard to tell without knowing the rest of the code, but if it's something
like this :

def show_lines(fileobj):
  for line in fileobj:
    print line

it will just work...

-- 
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'onurb at xiludom.gro'.split('@')])"



More information about the Python-list mailing list