title: Add __getitem__ function to COM object in Python, OR: Overriding __slots__?
body: <h2>Goal</h2>
  
  <p>I'm working with pywin32 and win32com to manipulate COM objects from a third-party application.</p>
  
  <p>One of my objects is a list, and I would like to iterate over the values in a list comprehension.</p>
  
  <h2>What works</h2>
  
  <p>With some of the COM objects I am able to successfully do things like:</p>
  
  <pre class="lang-py prettyprint-override"><code>&gt;&gt;&gt; [obj.Name for obj in myCom.someList if 'Test' in obj.Name]
  </code></pre>
  
  <p>And, as expected, I get a list of results:</p>
  
  <p><code>['Test1', 'Test2', 'Test3']</code></p>
  
  <h2>What doesn't...</h2>
  
  <p>However, on other similar list objects I try the same thing:</p>
  
  <pre class="lang-py prettyprint-override"><code>&gt;&gt;&gt; [obj.Name for obj in myCom.anotherList]
  Traceback (most recent call last):
    File "&lt;stdin&gt;", line 1, in &lt;module&gt;
    File "c:\Python27\lib\site-packages\win32com\gen_py\&lt;class_string&gt;.py", line 6575, in __iter__
      raise TypeError("This object does not support enumeration")
  TypeError: This object does not support enumeration
  </code></pre>
  
  <h2>What I've tried</h2>
  
  <p>So I try to add the function after defining <code>my_iter</code>:</p>
  
  <pre class="lang-py prettyprint-override"><code>&gt;&gt;&gt; myCom.anotherList.__iter__ = my_iter
  Traceback (most recent call last):
    File "&lt;stdin&gt;", line 1, in &lt;module&gt;
    File "c:\Python27\lib\site-packages\win32com\client\__init__.py", line 473, in __setattr__
      raise AttributeError("'%s' object has no attribute '%s'" % (repr(self), attr))
  AttributeError: '&lt;win32com.gen_py.Thingy instance at 0xDEADBEEF&gt;' object has no attribute '__iter__'
  </code></pre>
  
  <p>I tried the same thing with <code>__getitem__</code> and <code>next</code> and I get the same error. I did some reading and I assumed that this doesn't work because of something to do with <code>__slots__</code>... Not sure.</p>
  
  <h2>Examining generated Python source</h2>
  
  <p>After examining .py shown in the first stack trace, I looked at the definition of <code>class ISomeList.__iter__</code> - since that worked perfectly in the first example in the post.</p>
  
  <pre class="lang-py prettyprint-override"><code>from win32com.client import DispatchBaseClass
  LCID = 0x0
  class ISomeList(DispatchBaseClass):
      def __iter__(self):
          "Return a Python iterator for this object"
          try:
              ob = self._oleobj_.InvokeTypes(-4,LCID,2,(13, 10),())
          except pythoncom.error:
              raise TypeError("This object does not support enumeration")
          return win32com.client.util.Iterator(ob, '{&lt;&lt;&lt;guid&gt;&gt;&gt;}')
  </code></pre>
  
  <p>And compare that to <code>class IAnotherList.__init__</code>:</p>
  
  <pre class="lang-py prettyprint-override"><code>class IAnotherList(DispatchBaseClass):
      def __iter__(self):
          "Return a Python iterator for this object"
          try:
              ob = self._oleobj_.InvokeTypes(-4,LCID,3,(13, 10),())
          except pythoncom.error:
              raise TypeError("This object does not support enumeration")
          return win32com.client.util.Iterator(ob, None)
  </code></pre>
  
  <p>So, to me, it looks like assigning to <code>ob</code> fails and then we raise an error.</p>
  
  <p>I can modify the generated source to add the <code>__iter__</code> method, but that seems like a very "works on my machine" solution. What can I do?</p>