title: Python bytearray() index assignment
body: <p>Bear with me here; I am a sysadmin not a developer. The following code works just fine for me. But when I break it into two files so that the class is in one file and the logic is in another I get an error that data[0] is a str and does not support assignment</p>
  
  <p>Striped down working example</p>
  
  <pre><code>class partition:
      def __init__(self):
          self.data = bytearray(b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00")
          return
  
      def boot_flag(self, value=None):
          if value is not None:
              self.data[0] = value
              return
          else:
              return self.data[0:1][::-1]
  
  
  part1 = partition()
  print str(part1.data).encode('hex')
  value = b"\xff"
  part1.boot_flag(value)
  print str(part1.data).encode('hex')
  </code></pre>
  
  <p>This is the full class as it stands right now.</p>
  
  <pre><code>class partition:
      def __init__(self):
          self.data = bytearray(b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00")
  
      def boot_flag(self, value=None):
          if value is not None:
              self.data[0] = value
              return
          else:
              return self.data[0:1][::-1]
  
      def start_chs(self, value=None):
          if value is not None:
              self.data[1:4] = value
              return
          else:
              return self.data[1:4][::-1]
  
      def type(self, value=None):
          if value is not None:
              self.data[4:5] = value
              return
          else:
              return self.data[4:5][::-1]
  
      def end_chs(self, value=None):
          if value is not None:
              self.data[5:8] = value
          else:
              return self.data[5:8][::-1]
  
      def start_lba(self, value=None):
          if value is not None:
              self.data[8:12] = value
          else:
              return self.data[8:12][::-1]
  
      def sectors(self, value=None):
          if value is not None:
              self.data[12:16] = value
          else:
              return self.data[12:16][::-1]
  
      def load(self, data):
          self.data = data
          return
  </code></pre>
  
  <p>This is the test jig I am using to test the class. </p>
  
  <pre><code>import dospart
  disk = open('/dev/sda', 'rb')
  mbr = disk.read(512)
  part1 = dospart.partition()
  part1.load(mbr[446:462])
  print str(part1.data).encode('hex')
  part1.boot_flag(b"\xff")
  print str(part1.data).encode('hex')
  </code></pre>
  
  <p>This is the error </p>
  
  <pre><code>Traceback (most recent call last):
    File "test.py", line 13, in &lt;module&gt;
      part1.boot_flag(b"\xff")
    File "/Users/digitaladdictions/PycharmProjects/dospart/dospart.py", line 9, in boot_flag
      self.data[0] = value
  TypeError: 'str' object does not support item assignment
  </code></pre>
  
  <p>Note that I can read the values just fine. I only get an error when I try to write to self.data[]</p>
  
  <p>[UPDATE]</p>
  
  <p>Based on the accepted answer this is my new load function which works. </p>
  
  <pre><code>def load(self, data):
      part = bytearray(data)
      self.data = part
      return
  </code></pre>