Check if variable is an instance of a File object

Marc 'BlackJack' Rintsch bj_666 at
Fri Sep 15 09:51:11 CEST 2006

In <1158304694.673361.62600 at>, sc_wizard29

> 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 ?

Why do you want to be sure?  I would even rename the argument:

def show_lines(lines):
    for line in lines:

This works with *every* iterable that contains "lines".  No need to
"cripple" it with a type checking.

> #2 : should I do nothing ? (but I don't like the idea of risking to
> have a runtime exception raised somewhere)

Then don't use Python.  Except `SyntaxError` almost every
exception is a runtime one.

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?  ;-)

	Marc 'BlackJack' Rintsch

More information about the Python-list mailing list