2

Suppose I want to add some custom behavior to a file object, such as returned by open(). According this answer, there is (or was) no single base type for all file-like objects. Is this really still true? How about io.IOBase, or one of its subclasses, as suggested here? Or perhaps by now there is an abstract base class that I could use?

I have already looked at this question, but (besides being quite old) the issues there are about customizing the close() method (best done with a context manager). So this does not answer my question (and please don't mark this question as a duplicate of it!).

alexis
  • 48,685
  • 16
  • 101
  • 161
  • What "custom behavior" are you trying to add? – Jab Jan 16 '19 at 21:34
  • 2
    Well, the type returned by `open` depends on how you called it... – wim Jan 16 '19 at 21:35
  • 2
    Is there a reason you *need* a base class? Thanks to the magic of duck typing, it shouldn't need to matter (particularly if you just wrap the underlying object and pass through any calls you don't recognize). I can think of several other approaches to building type-agnostic wrappers as well. – Charles Duffy Jan 16 '19 at 21:45
  • Ideally I want to know a base class from which I can derive a subclass, so I can avoid playing tricks like overriding base classes etc. So it's more about the creation type... duck typing is for the consumer. With this question I'm asking for information about the situation with file objects. Maybe the answer is still "open file objects come in varieties that have nothing in common", maybe it's not. – alexis Jan 16 '19 at 22:34

0 Answers0