Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><code>isinstance(obj, some_class)</code> just iterates up obj's inheritance chain, looking for <code>some_class</code>. Thus <code>isinstance(file_like, io.IOBase)</code>, will be false, as your <code>file_like</code> class doesn't have <code>io.IOBase</code> in its ancestry. <code>file_like</code> doesn't designate an explicit parent, hence it implicitly inherits only from <code>object</code>. That's the only class - besides <code>file_like</code> itself - that will test positive for a <code>file_like</code> instance with <code>isinstance()</code>.</p> <p>What you are doing in <code>file_like</code> is defining the methods expected on a file-like object while not inheriting from any particular "file-like" class. This approach is called <a href="http://en.wikipedia.org/wiki/Duck_typing" rel="nofollow">duck-typing</a>, and it has many merits in dynamic languages, although it's more popular in others (e.g. Ruby) than Python. Still, if whatever you're providing your <code>file_like</code> instance to follows duck-typing, it should work, provided your <code>file_like</code> does in fact "quack like a file", i.e. behaves sufficiently like a file to not cause errors upon usage at the receiving end.</p> <p>Of course, if the receiving end is not following duck-typing, for example tries to check types by <code>isinstance()</code> as you do here, this approach will fail.</p> <p>Finally, a small stylistic nit: don't put empty parens on a class if it doesn't inherit anything explicitly. They are redundant.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

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