title: implement a custom open() function / 'subclass' the open() function
body: <p>Disclaimer: The word <strong>subclass</strong> in the title is not correct, but this is a reference to the old pytho2 only solution and I didn't know any better way of describing it.</p>
  <p><strong>The objective:</strong>
  I am looking for a way, that lets me do following in my existing code</p>
  <ul>
  <li>import <code>MyFile</code> at the beginning of a file</li>
  <li>'just' replace <code>open</code> with either <code>MyFile</code></li>
  <li>replace <code>open(*args, *kwargs)</code> with <code>MyFile(*args, *kwargs, special_param=True)</code></li>
  </ul>
  <p>The code should behave <strong>exactly</strong> as before, but trigger some additional behavior at the opening time (before or after opening) of the file and when some methods are called (e.g. <code>close()</code> before or after closing)</p>
  <p>With <strong>exactly</strong> as before I mean, that the code is still working if I would replace globally <code>open(</code> with <code>MyFile(</code> (and add of course the import for <code>MyFile</code></p>
  <p><code>open</code> is not a class and can therefore not be subclassed. The old python2 only version however was implemented by <em>subclassing</em> <code>file</code> (which is also not a class AFAIK, but the solution worked)</p>
  <p><strong>The old python2 only solution:</strong></p>
  <pre><code>class MyFile(file):
      def __init__(self, fname, mode, special_param=False):
          # next line would be nice, but is not essential
          # do_something_special_before_opening(self, special_param)
          super(MyFile, self).__init__(fname, mode)
          do_something_special_after_opening(self, special_param)
      
      def close(self):
          super(MyFile, self).close()
          do_something_special_after_closing(self)
  
  </code></pre>
  <p>The way it would be used is:</p>
  <pre><code>from mymodule import MyFile
  # and then use MyFile in the rest of the file
  </code></pre>
  <p>or</p>
  <pre><code>from mymodule import MyFile as open
  # keep the rest of the file unchanged
  </code></pre>
  <p>As <code>file</code> doesn't exist anymore in python.</p>
  <p>There is already an answer on SO ( <a href="https://stackoverflow.com/questions/47838405/porting-a-sub-class-of-python2-file-class-to-python3">Porting a sub-class of python2 &#39;file&#39; class to python3</a> )
  However <strong>this does not answer my question</strong>, as it suggests to inherit from <code>io.FileIO</code>, as it expects <code>bytes</code> to be written and not <code>str</code>ings.</p>
  <p>What is the best way to implement such a feature in python3 (should work for Linux and Windows)</p>
  <p>In python3 the result of <code>open()</code> does not always belong to the same class.</p>
  <p>Example:
  If opening in text mode a <code>_io.TextIOWrapper</code> object is returned. If opening in binary write mode a <code>_io.BufferedWriter</code> or <code>_io.BufferedReader</code> is returned. If opening in binary mode with <code>buffering=0</code> then <code>_io.FileIO</code> is returned.</p>
  <p>So one option could be to subclass <code>_io.TextIOWrapper</code>, <code>_io.BufferedWriter</code>, <code>_io.BufferedReader</code>, <code>_io.FileIO</code>, ... and create a factory returning one of these sub classes.</p>
  <p>However I wonder if there's a more elegant way. With above approach I had to create quite some sub classes and to reimplement the entire logic of open, that determines which class to use for which combination of parameters.</p>
  <p>Perhaps there is some trick with monkeypatching?</p>