[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: > ...
OT : this will shadow the builtin file type. Avoid shadowing builtin types if you value your mental sanity !-) > What can I do to be sure that the input argument is indeed a 'File' > object ? Why do you think you need "to be sure that the input argument is indeed a 'File' object" ? As long as the object passed in is able to answer to the messages you'll send to it, everything's fine, isn't it ? And if it fails, well, you'll find out pretty soon... > #1 : should I start by checking that 'file' is indeed an instance of a > File object ? Unless you have a *very* compelling reason to do so (and I can't imagine one here), definitively, no. FWIW, it's pretty common in Python to pass file-like objects (StringIo comes to mind... but there are lots of other cases) to functions expecting a file object. And remember that Python doesn't relies on inheritence for typing. Also, for what you showed of your code, any iterable seems ok !-) > (and how do I do this ?) isinstance(obj, *classes) OT : note that in your case, since the arg name 'file' shadows the builtin type, you'll have hard time using it !-) > #2 : should I do nothing ? Yes. Well, IMHO. > (but I don't like the idea of risking to > have a runtime exception raised somewhere) Err... What exactly do you intend to do if the object passed in is not a file ? Raise a TypeError ? If so, when do you think it will fires ? Yes my friend- at runtime !-) As you noticed, switching from Java to Python requires some mental adjustements... Not fearing runtime exceptions is one of them. Thinking in terms of implied interface is another one. In your above code, you really don't care if the objects is a file or not - you just care if the object support the subset of the file interface you intend to use. And this, you'll only know at runtime. -- bruno desthuilliers python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for p in '[EMAIL PROTECTED]'.split('@')])" -- http://mail.python.org/mailman/listinfo/python-list