Sequences/lists/enumerations in COM, Python 2.1, Windows 2000

Paul Boddie paul at
Thu May 31 05:30:23 EDT 2001


I have recently started using a Windows 2000 environment seriously and
have felt the need to experiment a little with Python's COM support.
However, the documentation and behaviour of sequences is somewhat

Having read the source code for some of the examples, and having seen
some interesting comp.lang.python posts about MAPI, I attempted to
automate Outlook. The first issue came up with traversing folder

  # Get a namespace.

  outlook = win32com.client.Dispatch("Outlook.Application")
  ns = outlook.GetNamespace("MAPI")
  top_level = ns.Folders
  first_folder = top_level.GetFirst()
  second_folder = top_level.GetNext()

  # Go down a level.

  first_sub_folder = second_folder.GetFirst()
  second_sub_folder = second_folder.GetNext()

  # Now, pay attention!

  third_sub_folder = second_folder.GetNext()

  # This will be the *same* object as the second_sub_folder!

Actually, the GetLast method does yield the last folder in a
collection of folders, and the GetPrevious method always yields None
after that. Anyway, after studying some examples, I found that one can
use Python's index operations on the collections, which leads me to my
second problem:

  first_folder = top_level[0] # Yields an exception - there is
                              # no folder 0!
  first_folder = top_level[1] # Works - it's the first folder
                              # alright!

  # This breaks all known Python idioms, surely...

  len(top_level)              # Gives the number of folders and
                              # is equal to top_level.Count
  top_level[len(top_level)]   # Gives the last element in the
                              # sequence.
  top_level[-1]               # Doesn't work!

Given that the index notation can be used, I don't really care about
the dubious GetFirst, GetNext methods, but what I would like to know
is: why don't sequence indexes behave "normally" for Python? Is it a
Python 2.1 issue, a win32com issue, or just something strange with
Outlook and the Exchange server I'm using? Also, would iteration over
sequences be supported once iterator support is built in to Python?



More information about the Python-list mailing list