Thursday, 15 April 2010

iterate over a list as iterator in python -


I have a function that gives a list of objects (for example I used the code below). Each object has the attribute of the text:

  def mylist (): mylist = [] for the category (5): elem = myobject (i) mylist.append (elem) mylist For obj return mylist (): obj.text print  

I can rewrite this code so that mylist () returns the new value of each iteration and I iterator iterate over ? In other words, how can I reuse my list in Python so that use xchange ()?

If I think right, you are looking for:

  

Code> class myobject: def __init __ (self, i): self.text = 'hello' + str (i) def mylist (): i in range (5): elem = myobject (i) mylist () for the object : Print obj.text


No comments:

Post a Comment