Hi Danie,

The enumerate() function takes a list of elements, and returns a list of
(index, element) pairs.  For example:

######
>>> names = ["wilma", "fred", "betty", "barney"]
>>> for p in enumerate(names):
...     print p
...
(0, 'wilma')
(1, 'fred')
(2, 'betty')
(3, 'barney')
######

Note that nothing stops us from doing an iteration directly across a list:
there is no need to "enumerate" if we don't care about indices:

######
>>> for n in names:
...     print n.capitalize()
...
Wilma
Fred
Betty
Barney
######


Let's look in the code that you have:

>     #manipulate object in list
>       for p in enumerate(range(10)):
>               myObject=p
>               print myObject.getName()

This code is buggy because the enumeration is going across an arbitrary
list of the integers between zero and ten.  Python has no clue that there
should be a relationship here with the code you had previously with the
'list' collection.


You may want to iterate across the 'list' that you've constructed instead.

######
for object in list:
    print object.getName()
######

This has a similar effect to the following Java pseudocode:

/*** Java pseudocode ***/
for (Iterator iter = list.iterator(); iter.hasNext(); ) {
    object = (MyObject) iter.next();
    System.out.println(object.getName());
}
/******/

except you don't have to worry about the type casting in Python.


I hope this helps!

_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to