title: Python dataclasses: assign variable to field in a frozen instance from input
body: <p>I want to create an <strong>immutable</strong> class that reads a file and do other things. I have problems with the mutability:</p>
  <pre><code>from dataclasses import dataclass
  
  import io
  
  
  @dataclass(frozen=True)
  class Book:
      
      filename: str
      #file: io.TextIOWrapper
      
      def __new__(cls, filename):
          
          self = super().__new__(cls)
          
          self.file = open(filename, &quot;r&quot;)
          
          return self
      
      
      def __post_init__(self):
          
          #self.file = open(self.filename, &quot;r&quot;)
          
          pass
      
      
      def close(self):
          
          self.file.close()
  
  book = Book(&quot;testfile.txt&quot;)
  book.close()
  print(book)
  </code></pre>
  <p>This is the error I get:</p>
  <pre><code>Traceback (most recent call last):
    File &quot;D:\Sync1\Code\Python3\EconoPy\Version_0.2\test.py&quot;, line 32, in &lt;module&gt;
      book = Book(&quot;testfile.txt&quot;)
    File &quot;D:\Sync1\Code\Python3\EconoPy\Version_0.2\test.py&quot;, line 17, in __new__
      self.file = open(filename, &quot;r&quot;)
    File &quot;&lt;string&gt;&quot;, line 4, in __setattr__
  dataclasses.FrozenInstanceError: cannot assign to field 'file'
  </code></pre>
  <p>I want to set the attribute <code>self.file</code> from the input <code>filename</code>, but the 'frozening' is forbidding that. With the <code>__post_init__</code> I can do that if I remove the 'frozening'.</p>