title: setitem and getitem -- python
body: <p>I've created a python program that makes a vector. Now i want to set an item using a function<code>__setitem__</code> and<code>__getitem__</code>. So for example, if<code>vector = Vec()</code>and<code>vector[3] = 26</code>would change an empty vector to<code>[0, 0, 0, 26]</code>. I need to override the<code>__getitem__</code>and<code>__setitem__</code> I've listed the code below, but i'm having troubles with the get and set functions. Any advice?</p>
  
  <pre><code>class Vec:
      def __init__(self, length = 0):
          self.vector = [0]*length
  
      def __str__(self):
          return '[{}]'.format(', '.join(str(i) for i in self.vector))
          #This formats the output according to the homework.
          #Adds '[' and ']' and commas between each 0
  
      def __len__(self):
          return len(self.vector)
  
      def extend(self, newLen):
          self.vector.append([0]*newLen)
          return (', '.join(str(j) for j in self.vector))
  
      def __setitem__(self, key, item):
          self.vector[key] = item
  
      def __getitem__(self, key):
          return self.vector[key]
  </code></pre>