How can the for loop in Python iterate through objects that I cannot address using the [n] notation?
Consider this:
myCollection # Some objects with elements (not a simple list)
for elem in myCollection:
print elem.Title
myCollection[0]
The code above would in my case succeed in the for loop and will print the title string of all the elements, while the call to myCollection[0] would fail with the following exception:
TypeError: 'myCollection' object is unsubscriptable
How does the for statement iterate through the objects?
Is there another way to access the first element of the collection when the subscript notation fails?
Background
This comes up in IronPython scripting in the Spotfire application which is why I cannot give a MWE.
Here is a dir(myCollection):
['Equals', 'GetHashCode', 'GetType', 'Item', 'MemberwiseClone', 'Overloads', 'ReferenceEquals', 'ToString', '__call__', '__class__', '__cmp__', '__delattr__', '__delete__', '__doc__', '__get__', '__getattribute__', '__getitem__', '__hash__', '__init__', '__module__', '__name__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__self__', '__setattr__', '__str__']
to illustrate the methods defined on this object. It does not have a next or next method and for loops still work here.