isinstance(.., file) for Python 3
stefan_ml at behnel.de
Thu Nov 8 16:14:37 CET 2012
Duncan Booth, 08.11.2012 14:58:
> Ulrich Eckhardt wrote:
>> If possible, I'm looking for a solution that works for Pythons 2 and 3,
>> since I'm not fully through the conversion yet and have clients that
>> might use the older snake for some time before shedding their skin.
> Why bother checking types at all?
> def foo(file_or_string):
> data = file_or_string.read()
> except AttributeError:
> data = file_or_string
> ... use data ...
Or, a tiny bit more safely:
read = file_or_string.read
data = file_or_string
data = read()
I'd rather go with one of the previous solutions, though.
More information about the Python-list