# It can be useful to iterate through data contained # in your own custom objects. # Lets say you have your own class class ExampleClass(object): def __init__(self): self.objectList = [] self.objectDict = {} self.maxItem = 100 self.objectItem = "" def iterateList(self): return self.objectList def addListItem(self, item): self.objectList.append(item) def addDictItem(self, item, value): self.objectDict[item] = value # create an instance of the class # and lets use it's iterating methods ec = ExampleClass() # add some example data for i in xrange(10): ec.addListItem(i) ec.addDictItem(i, str(i)+"'s value") # now that we have data lets iterate # through the data for item in ec.iterateList(): print item #output: # 0 # 1 # 2 # 3 # 4 # 5 # 6 # 7 # 8 # 9
A python example based blog that shows how to accomplish python goals and how to correct python errors.
Sunday, October 4, 2009
Python - make your own class attributes iterable
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment