Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Checking <code>isinstance(something, io.IOBase)</code> only checks if <code>something</code> is an instance of an <code>io.IOBase</code> or a class derived from it — so I don't understand where you got the mistaken idea that it's the "correct" way to determine if an object is "file-like".</p> <p>A different way to do it is with an <em>Abstract Base Class</em>. Python has a number of <a href="http://docs.python.org/3/library/collections.abc.html?highlight=collections%20abstract%20base%20classes" rel="nofollow noreferrer">built-in ones</a>, but currently doesn't have one for "file-like" that could used with <code>isinstance()</code>. However you can define your own by using the <code>abc</code> module as outlined in <a href="http://www.python.org/dev/peps/pep-3119/#id34" rel="nofollow noreferrer">PEP 3119</a>.</p> <p><a href="http://pymotw.com/2" rel="nofollow noreferrer">The Python Module of the Week</a> webiste has a <a href="http://pymotw.com/2/abc" rel="nofollow noreferrer">good explanation</a> of using the <code>abc</code> module to do things like as this. And this highly rated <a href="https://stackoverflow.com/a/306222/355230">answer</a> to the question <a href="https://stackoverflow.com/questions/305359/correct-way-to-detect-sequence-parameter">Correct way to detect sequence parameter?</a> shows a similar way of defining your own ABC.</p> <p>To illustrate applying it to your case, you could define an ABC like this with all its methods abstract — thereby forcing derived classes to define all of them in order to be instantiated:</p> <pre><code>from abc import ABCMeta, abstractmethod class ABCFileLike(metaclass=ABCMeta): @abstractmethod def __init__(self): pass @abstractmethod def write(self, line): pass @abstractmethod def close(self): pass @abstractmethod def flush(self): pass </code></pre> <p>You could then derive your own concrete classes from it, making sure to supply implementations of all the abstract methods. (If you don't define them all, then a <code>TypeError</code> will be be raised if any attempts are made to instantiate it.)</p> <pre><code>class FileLike(ABCFileLike): """ Concrete implementation of a file-like class. (Meaning all the abstract methods have an implementation.) """ def __init__(self): pass def write(self, line): print("Written:", line) def close(self): pass def flush(self): pass print(isinstance(FileLike(), ABCFileLike)) # -&gt; True </code></pre> <p>You can even add existing classes to it by registering them with the new metaclass:</p> <pre><code>import io print(isinstance(io.IOBase(), ABCFileLike)) # -&gt; False ABCFileLike.register(io.IOBase) print(isinstance(io.IOBase(), ABCFileLike)) # -&gt; True </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload